我尝试在使用WHERE NOT EXISTS之后在mysql语句中使用AND,但它不适合我,继承人我在哪里
SELECT * from ".$usertable." u where not exists (select * from ".$usermetatable." m where m.meta_key = 'course_progress' and m.user_id = u.ID ) AND m.meta_value = locationvalue";
或者我必须在WHERE NOT EXIST之前添加它吗?
答案 0 :(得分:0)
问题在于:
SELECT *
from ".$usertable." u
where not exists (select * from ".$usermetatable." m where m.meta_key = 'course_progress' and m.user_id = u.ID )
AND m.meta_value = locationvalue";
您正在引用' m'但是' m'括在括号中
它应该更像是:
SELECT *
from ".$usertable." u
where not exists (select * from ".$usermetatable." where meta_key = 'course_progress' and user_id = u.ID ) m
AND m.meta_value = locationvalue";
如果你担心性能问题,我不会在mysql或任何子查询上使用,而mysql优化器在这方面非常糟糕。
这样的事情应该更有效率(未经测试,可能有拼写错误):
SELECT *
FROM ".$usertable." u
LEFT OUTER JOIN (select * from ".$usermetatable." where meta_key = 'course_progress' ) m
ON m.user_id = u.ID
AND m.meta_value = u.locationvalue
WHERE m.user_id IS NULL