表 “规划”凡亚子查询的MySQL
id name 1 abc 2 def 3 ghi
表 “时间表”
id fk_store fk_plan 1 4 1 2 4 1 3 4 1 4 6 1 5 6 1 6 5 3 7 7 3 8 7 3 9 12 1
这是我的查询:
SELECT plan.id as id, name,
(SELECT GROUP_CONCAT(cnt) cnt FROM (SELECT COUNT(*) cnt
FROM schedule WHERE fk_plan = plan.id GROUP BY fk_store) q) as schedule_count
FROM plan LEFT JOIN schedule ON plan.id = schedule.fk_plan GROUP BY plan.id
我期待这样的结果(例如,在阵列):
[0] = array(
['id'] = 1,
['name'] = 'abc',
['schedule_count'] = '3, 2, 1'
),
[1] = array(
['id'] = 2,
['name'] = 'def',
['schedule_count'] = ''
),
[2] = array(
['id'] = 3,
['name'] = 'ghi',
['schedule_count'] = '2,1'
),
不幸的是我得到的误差,在WHERE子句是未定义的列plan.id。当我不使用WHERE结果'schedule_count'是'3,2,2,1,1'在每个数组中。我发现了许多类似的解决方案,但我无法修复它。
我希望我的问题很清楚。先谢谢你。
plan.id其实并不存在,你调用它,因为它会如果你使用的是连接(我不能告诉是否有可能在您的情况) –
@Bartdude是现在我知道它不存在。但一切都是可能的,所以我期望这有一个解决方案 – mesnicka