有3张桌子。
items_table:
iid
ititle
itext
tag_list_table
tid
tname;
tag_ref_table
iid
tid //foreign key for tag_list_table
tag_list_table中的每条记录都有一个或多个标记。
for example:
tag_list_table:
iid=1,itltle="this is title";itext="this is full text";
iid=2,itltle="this is title2";itext="this is full text2";
tag_list_table
tid=1 tname="red"
tid=2 tname="green"
tid=3 tname="yellow"
tid=4 tname="orange"
tag_ref_table
iid=1 tid=1
iid=1 tid=2
iid=1 tid=3
iid=2 tid=4
我希望得到这样的结果:
ROW1: 1- 这是标题 这是全文 红,绿,黄
ROW2: 2- 这是title2 这是全文2 橙
我试过这些:
SELECT i.ititle,i.itext,t.tname
FROM tag_ref_table as i
LEFT JOIN tag_ref_table t WHERE tid=iid
LEFT JOIN tag_list_table r WHERE tid=??????....
答案 0 :(得分:2)
您可以在选择中使用 CONCAT() 和 GROUP_CONCAT() :
SELECT CONCAT(t1.iid,'- ',t1.ititle,' ',t1.itext,' ',t1.tname) FROM
(SELECT i.iid,i.ititle,i.itext,GROUP_CONCAT(r.tname) tname
FROM items_table i
INNER JOIN tag_ref_table t ON i.iid = t.iid
INNER JOIN tag_list_table r ON t.tid = r.tid
GROUP BY i.ititle,i.itext,i.iid)t1;
您可以在以下网址看到完整的答案:
答案 1 :(得分:2)
您正在寻找GROUP_CONCAT
SELECT it.ititle, it.itext, GROUP_CONCAT(tlt.tname)
FROM tag_ref_table as trt
INNER JOIN items_table it ON trt.iid = it.iid
INNER JOIN tag_list_table tlt ON trt.tid = tlt.tid
GROUP BY it.ititle, it.itext;
除非您的tag_ref_table
Junction表允许使用NULLS(在这样的表中不常见),否则我建议切换到INNER JOIN。
答案 2 :(得分:2)
如果将“红色,绿色,黄色”值连接成一个字符串,在MySQL中你可以像这样使用GROUP_CONCAT函数:
SELECT i.*, group_concat(l.tname SEPARATOR ',') as names
FROM items_table as i
JOIN tag_ref_table r ON r.iid=i.iid
JOIN tag_list_table l ON r.tid= l.tid
GROUP BY i.iid
文档:http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
更多示例:http://www.giombetti.com/2013/06/06/mysql-group_concat/
答案 3 :(得分:1)
DROP TABLE items;
CREATE TABLE items
(item_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,item_title VARCHAR(20)
,item_text VARCHAR(20) NOT NULL
);
DROP TABLE tags;
CREATE TABLE tags
(tag_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,tag_name VARCHAR(12) NOT NULL
);
DROP TABLE items_tags;
CREATE TABLE items_tags
(item_id INT NOT NULL
,tag_id INT NOT NULL
,PRIMARY KEY(item_id,tag_id)
);
INSERT INTO items VALUES
(1,"this is title1","this is full text1"),
(2,"this is title2","this is full text2");
INSERT INTO tags VALUES
(1,"red"),
(2,"green"),
(3,"yellow"),
(4,"orange");
INSERT INTO items_tags VALUES
(1,1),
(1,2),
(1,3),
(2,4);
mysql> SELECT * FROM items;
+---------+----------------+--------------------+
| item_id | item_title | item_text |
+---------+----------------+--------------------+
| 1 | this is title1 | this is full text1 |
| 2 | this is title2 | this is full text2 |
+---------+----------------+--------------------+
2 rows in set (0.00 sec)
SELECT * FROM tags;
+--------+----------+
| tag_id | tag_name |
+--------+----------+
| 1 | red |
| 2 | green |
| 3 | yellow |
| 4 | orange |
+--------+----------+
SELECT * FROM items_tags;
+---------+--------+
| item_id | tag_id |
+---------+--------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 4 |
+---------+--------+
SELECT i.*
, GROUP_CONCAT(t.tag_name ORDER BY t.tag_id) tags
FROM items i
JOIN items_tags it
ON it.item_id = i.item_id
JOIN tags t
ON t.tag_id = it.tag_id
GROUP
BY i.item_id;
+---------+----------------+--------------------+------------------+
| item_id | item_title | item_text | tags |
+---------+----------------+--------------------+------------------+
| 1 | this is title1 | this is full text1 | red,green,yellow |
| 2 | this is title2 | this is full text2 | orange |
+---------+----------------+--------------------+------------------+
请注意,GROUP / GROUP_CONCAT部分在应用程序级别也可以执行(如果不是更好)(例如,一个简单的PHP循环作用于有序数组)