我遇到以下问题,当我尝试group_concat列时,我的查询停止工作,并说Unknown column 'dbname.tablename.columnname' in 'where clause'
。我将尝试总结工作查询和非工作以及我的最佳能力:
工作查询:
SELECT
table1.items,
table2.opt1,
table3.opt2
FROM table1
LEFT JOIN table2 ON table1.id = table2.table1_id
...
LEFT JOIN table3 ON table2.id = table3.table2_id
WHERE
...
GROUP BY table1.items
HAVING
(SELECT COUNT(*) FROM table3 WHERE table3.table2_id = table2.id AND (table3.opt2 = 1)) = (1)
非工作查询:
SELECT
table1.items,
table2.opt1,
GROUP_CONCAT(table3.opt2 SEPERATOR ', ') as concat_opt2
FROM table1
LEFT JOIN table2 ON table1.id = table2.table1_id
...
LEFT JOIN table3 ON table2.id = table3.table2_id
WHERE
...
GROUP BY table1.items
HAVING
(SELECT COUNT(*) FROM table3 WHERE table3.table2_id = table2.id AND (table3.opt2 = 1)) = (1)
工作查询按预期返回结果。现在我想group_concat他们:} table3.opt2,所以当我添加group_concat时,我得到错误unknown column 'dbname.table2.id'
我怀疑我在条款中得到这个错误...
编辑1:在HAVING之前的WHERE之后添加GROUP BY table1.items
(仍然会收到相同的错误)。