我有以下表格
我的初步查询如下
select questions.id, questions.question_data, users.id, users.fname from questions, users where questions.user_id = users.id limit 30
但是在这里,我想要那个问题的用户数量,所以我尝试了以下查询
select questions.id, questions.question_data, users.id, users.fname, count(questions_connect.id) from questions, users LEFT JOIN questions_connect ON `questions`.`id` = `questions_connect`.`question_id` where questions.user_id = users.id group by `questions_connect`.`id` limit 30
这显示错误
Unknown column 'questions.id' in 'on clause'
我们可以通过自然加入和左连接进行1次调用,如果是,我会在哪里出错..?
答案 0 :(得分:1)
使用显式连接应该排除你:
select questions.id, questions.question_data, users.id, users.fname, count(questions_connect.id)
from questions
join users on questions.user_id = users.id
left join questions_connect on `questions`.`id` = `questions_connect`.`question_id`
group by `questions_connect`.`id`
limit 30
最好明确指定所有连接,尽量忘记隐式连接存在。
答案 1 :(得分:0)
我相信你不需要在联接上加上引号
ON questions.id = questions_connect.question_id