用 find_in_set 而不是 like再说即便有 #,那么 like '%#2%' 也是会匹配到 #2,,#21,#201,....
所以才 #开头 ,结尾 这样子去分辨 然后 like '%#2,%'这是我的代码 麻烦帮看下有多糟糕 - -select * from ".文章表." left join ".作者表." on ".作者表.".zuozhe_id in (REPLACE(".文章表.".zuozhe_id, '#', '')) left join ".标签表." on ".标签表.".tag_id in (REPLACE(".文章表.".tag_id, '#', '')) where ".标签表.".tag like '%娱乐%' find_in_set(".标签表.".tag_id,REPLACE(".文章表.".tag_id, '#', '')) order by ".文章表.".id desc limit 10 请版主指教正确的代码
create temporary table `文章表` select 1 as id, '早上新闻' as title, '2,3' as tag_id union select 2, '午间30分', '3';create temporary table `标签表` select 1 as tag_id, '军事' as tag union select 2, '娱乐' union select 3, '明星';select title, group_concat(tag) from `文章表` left join `标签表` on find_in_set(`标签表`.tag_id,`文章表`.tag_id) group by id;select tag, title from `标签表` left join `文章表` on find_in_set(`标签表`.tag_id,`文章表`.tag_id) title group_concat(tag) 早上新闻 明星,娱乐 午间30分 明星 tag title 军事 娱乐 早上新闻 明星 早上新闻 明星 午间30分
id title tag_id
1 早上新闻 2,3
1 午间30分 3连接条件:
find_in_set(标签表.tag_id, 文章表.tag_id)
tag_id 列中的 # 是为了区分标签很多的时候 比如 #12,#2, 如果去掉#号 我查询时 like '%2%' 就会把 12也带上
#2,,#21,#201,....
所以才 #开头 ,结尾 这样子去分辨 然后 like '%#2,%'这是我的代码 麻烦帮看下有多糟糕 - -select * from ".文章表." left join ".作者表." on ".作者表.".zuozhe_id in (REPLACE(".文章表.".zuozhe_id, '#', '')) left join ".标签表." on ".标签表.".tag_id in (REPLACE(".文章表.".tag_id, '#', '')) where ".标签表.".tag like '%娱乐%' find_in_set(".标签表.".tag_id,REPLACE(".文章表.".tag_id, '#', '')) order by ".文章表.".id desc limit 10
请版主指教正确的代码
select 1 as id, '早上新闻' as title, '2,3' as tag_id
union select 2, '午间30分', '3';create temporary table `标签表`
select 1 as tag_id, '军事' as tag
union select 2, '娱乐'
union select 3, '明星';select title, group_concat(tag) from `文章表` left join `标签表` on find_in_set(`标签表`.tag_id,`文章表`.tag_id) group by id;select tag, title from `标签表` left join `文章表` on find_in_set(`标签表`.tag_id,`文章表`.tag_id)
title group_concat(tag)
早上新闻 明星,娱乐
午间30分 明星
tag title
军事
娱乐 早上新闻
明星 早上新闻
明星 午间30分
不然写了你也套不进去
谢谢版主 我就专门建了个小表 麻烦版主帮一下 想要的结果就是两种
第一种:
标题 作者 分类
今日晨报 小明/芳芳 明星/军事
午间30分 小明 娱乐/明星
第二种:
显示分类下的文章
娱乐 明星 军事
午间30分(小明) 今日晨报(小明/小芳) 今日晨报(小明/小芳)
<li><a href="1.php>"今日晨报</a> (<a href="2.php">小明</a>/<a href="3.php">芳芳</a>) </li>
以下是我的表结构SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;--
-- 数据库: `news`
---- ----------------------------------------------------------
-- 表的结构 `tag`
--CREATE TABLE IF NOT EXISTS `tag` (
`tag_id` int(11) NOT NULL,
`tag_name` varchar(225) NOT NULL,
`tag_color` varchar(225) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;--
-- 转存表中的数据 `tag`
--INSERT INTO `tag` (`tag_id`, `tag_name`, `tag_color`) VALUES
(10, '军事', 'blue'),
(8, '娱乐', ''),
(9, '明星', 'red');-- ----------------------------------------------------------
-- 表的结构 `xinwen`
--CREATE TABLE IF NOT EXISTS `xinwen` (
`id` int(11) NOT NULL,
`title` varchar(225) NOT NULL,
`author_id` varchar(225) NOT NULL,
`tag_id` varchar(225) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;--
-- 转存表中的数据 `xinwen`
--INSERT INTO `xinwen` (`id`, `title`, `author_id`, `tag_id`) VALUES
(1, '今日晨报', '#2,#3,', '#9,#10,'),
(2, '午间30分', '#2,', '#9,#8,');-- ----------------------------------------------------------
-- 表的结构 `zuozhe`
--CREATE TABLE IF NOT EXISTS `zuozhe` (
`author_id` int(11) NOT NULL,
`author_name` varchar(225) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;--
-- 转存表中的数据 `zuozhe`
--INSERT INTO `zuozhe` (`author_id`, `author_name`) VALUES
(2, '小明'),
(3, '芳芳');
select title,
(select group_concat(author_name) from zuozhe where find_in_set(author_id, replace(xinwen.author_id,'#',''))) as author_name,
(select group_concat(tag_name) from tag where find_in_set(tag_id, replace(xinwen.tag_id,'#',''))) as tag_name
from xinwen
SQL;
$rs = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($rs)) {
$r1[] = $row;
}
print_r($r1);
Array
(
[0] => Array
(
[title] => 今日晨报
[author_name] => 小明,芳芳
[tag_name] => 军事,明星
) [1] => Array
(
[title] => 午间30分
[author_name] => 小明
[tag_name] => 娱乐,明星
))
第二种类同
<li><a id='1'>今日晨报</a> (<a author_id='2'>小明</a>/<a author_id='3'>芳芳</a>) <a tag_id='10'>军事</a>/<a tag_id='9'>明星</a> </li>标签对应标签id 作者对应作者id 这样怎么得到呢? 直接$row['tag_id']不对的吧?