抱歉我的英語不好,我需要從 mysql 4 表中獲取數據,但它只返回 1 行。表格1表 1 中的 col_2 包含數字數據作為表 2 col_2 的參考+----+-------+---------+| id | col_1 | col_2 |+----+-------+---------+| 1 | A | 4 || 2 | B | 5 || 3 | C | 6 |+----+-------+---------+表 2+----+-------+---------+| id | col_1 | col_2 |+----+-------+---------+| 1 | A | 4 || 2 | B | 5 || 3 | C | 6 || 4 | C | 7 || 5 | C | 8 |+----+-------+---------+表3表 3 col_1 具有表 1 id 的數值,但大多數主題將是相同的 id,col_2 具有表 4 的 id 數值+----+-------+---------+| id | col_1 | col_2 |+----+-------+---------+| 1 | 1 | 1 || 2 | 1 | 2 || 3 | 1 | 3 || 4 | 2 | 4 || 5 | 2 | 5 || 6 | 4 | 8 || 7 | 4 | 1 || 8 | 5 | 2 || 9 | 5 | 5 || 10 | 5 | 8 || 11 | 5 | 9 || 12 | 5 | 10 |+----+-------+---------+表 4+----+-------+| id | col_1 |+----+-------+| 1 | A || 2 | B || 3 | C |+----+-------+我運行以獲取數據的查詢。SELECT t1.id, t1.col_1, t2.col_1 as result_0, GROUP_CONCAT(t4.col_1) as resultFROM table_1 AS t1LEFT JOIN table_2 AS t2 ON t2.col_2 = t1.col_2LEFT JOIN table_3 AS t3 ON t3.col_1 = t1.idLEFT JOIN table_4 AS t4 ON t4.id = t3.col_2WHERE t1.col_2 > 1ORDER BY t1.id DESC如您所見,表 3 具有與表 1 和表 4 不同的 id,這兩個表都有值,所以我希望查詢返回如下所示。這意味著我希望表 4 中的值用逗號分隔。+----+-----------+-----------+-----------+| id | result_1 | result_2 | result_3 |+----+-----------+-----------+-----------+| 1 | A | A | A,B,C || 2 | B | B | A,B || 3 | C | C | C,B | +----+-----------+-----------+-----------+更新: 這里要求的是查詢的小提琴。SQL Fiddle http://sqlfiddle.com/#!9/3af0af/1 謝謝
1 回答

qq_花開花謝_0
TA貢獻1835條經驗 獲得超7個贊
根據您發布的小提琴
您所要做的就是將您的查詢更改為:-
SELECT
t1.id,
t1.name,
t2.role as result_0,
GROUP_CONCAT(t4.ct) as result
FROM
table_one AS t1
LEFT JOIN table_tow AS t2
ON t2.level = t1.level
JOIN table_three AS t3 //CHANGED LEFT JOIN TO JOIN
ON t3.vid = t1.id
LEFT JOIN table_four AS t4
ON t4.id = t3.cid
WHERE t1.level > 1
group by t1.id ORDER BY t1.id ASC //ADDED group by t1.id AND CHANGED ORDER BY t1.id DESC TO ORDER BY t1.id ASC
- 1 回答
- 0 關注
- 136 瀏覽
添加回答
舉報
0/150
提交
取消