以下是我的数据库结构:
assignments(assignment_id, year, semester, title, number, weighting, handout, handin, feedback, wordcount, briefd, profile_image, sub_details, add_note, author, user_id, module_id)
requests(request_id, assignment_id, allow, requestee)
我认为这将是一项简单的任务,我希望得到来自assignments.user_id的人员,他们在请求表中有请求。我尝试了以下但是我遇到了SQL错误:
SELECT COUNT(allow) FROM requests WHERE requests.assignment_id = assignments.user_id = 31
错误:
#1054 - Unknown column 'assignments.user_id' in 'where clause'
也许我需要加入?任何帮助表示赞赏。
答案 0 :(得分:1)
SELECT COUNT(allow)
FROM requests WHERE requests.assignment_id = assignments.user_id AND
assignments.user_id=31
但最好使用显式JOIN
SELECT count(assignments.user_id) FROM assignments
JOIN requests ON requests.assignment_id = assignments.assignment_id
WHERE assignments.user_id=31
答案 1 :(得分:0)
SELECT count(assignments.user_id)
FROM assignments
JOIN requests ON requests.assignment_id = assignments.assignment_id
WHERE assignments.user_id = 31
答案 2 :(得分:0)
您应该使用on来加入2个表而不是
SELECT COUNT(allow) FROM requests ON requests.assignment_id = assignments.user_id
WHERE assignments.user_id = 31
看看这个