如何解决聚集的联接表在MySQL 5上导致SQL SELECT
|| 我具有以下架构,并希望执行一个查询,以返回articles
表中每个条目的一行,并带有content
表中对应的content
列,以及包含该文章标签的每个列,例如您可能会使用concat
。
该查询应仅选择与特定标签匹配的行。因此,如果提供了标签“ 4”,结果集将类似于:
id content tags
1 on my way nails broke and fell song,atdi,invalid
3 im all alone so far up here and my oxygen is all gone song,hourglass
4 you know your insides true better than i do song,starslight
我已经尝试了几种不同的子查询方式,但是不断出现错误-这非常令人沮丧。
这是架构:
CREATE TABLE articles (
id int not null default 0,published datetime,author int not null default 0,primary key (id)
);
INSERT INTO articles
(id,published,author)
VALUES
(1,CURRENT_TIMESTAMP,1),(2,(3,(4,1);
CREATE TABLE content (
id int not null default 0,content varchar(250) not null default \'\',primary key (id)
);
INSERT INTO content
(id,content)
VALUES
(1,\'on my way nails broke and fell\'),\'exo skeleton junction at the railroad delayed\'),\'im all alone so far up here and my oxygen is all gone\'),\'you know your insides true better than i do\');
CREATE TABLE tags (
id int not null default 0,tag varchar(100) not null default \'\',primary key (id,tag)
);
INSERT INTO tags
(id,tag)
VALUES
(1,\"song\"),(1,\"atdi\"),\"mars\"),\"invalid\"),\"roulette\"),\"hourglass\"),\"starslight\");
解决方法
尝试这样的事情
select a.id,a.content,b.tags_1
from content as a inner join (
select id,GROUP_CONCAT(tag SEPARATOR \',\') as tags_1 FROM tags group by id
) as b on a.id = b.id
INNER JOIN tags AS c ON a.id = c.id
WHERE c.tag = \'atdi\'
使用GROUP_CONCAT()
方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。