我有一个包含库存记录的表,另一个包含描述每个库存记录的文档的表,例如:
inventory (t1)
t1id t1c1
1 desc1
2 desc2
3 desc3
documents (t2)
t2id t2c1 t1id
1 doc1 1
2 doc2 1
3 doc3 2
4 doc4 3
所以我尝试这个查询,我收到一条错误消息,告诉我子查询= s附近没有这样的列(t1.t1id)
SELECT t1c1,(SELECT GROUP_CONCAT(t2c1) FROM t2 WHERE t1.t1id = t2.t1id) FROM t1
有任何想法吗?
最佳答案
这适用于5.0和5.1:
原文链接:https://www.f2er.com/mysql/532007.htmlCREATE TABLE t1 (t1id INT NOT NULL,t1c1 VARCHAR(20) NOT NULL);
CREATE TABLE t2 (t2id INT NOT NULL,t2c1 INT NOT NULL,t1id INT NOT NULL);
SELECT t1c1,(
SELECT GROUP_CONCAT(t2c1)
FROM t2
WHERE t1.t1id = t2.t1id
)
FROM t1
您可以尝试将其重写为JOIN:
SELECT t1c1,GROUP_CONCAT(t2c1)
FROM t1
LEFT JOIN
t2
ON t2.t1id = t1.t1id
GROUP BY
t1.t1id