SELECT jobs.*, user_table.*
FROM jobs
INNER JOIN user_table
ON jobs.userid = user_table.userid WHERE
(user_table.userid = ".$_SESSION['userid']." AND approved = 1) OR
(user_table.userid = ".$_SESSION['userid']." AND approved = 2)
我过去常常使用它的方式是:
SELECT jobs.*, user_table.*
FROM jobs INNER JOIN user_table
ON jobs.userid = user_table.userid WHERE
user_table.userid = ".$_SESSION['userid']." AND approved = 1 OR approved = 2
第一个有效,但想知道是否有更短的路。第二个更短但不起作用,因为它拉出批准等于2的记录,但没有用户ID等于会话用户ID。
答案 0 :(得分:2)
您可以尝试使用IN语法:
WHERE user_table.userid = ".$_SESSION['userid']."
AND approved IN (1,2)
如果您在usertable(userid, approved)
上创建复合索引,此查询将利用该索引(如果approved
位于user_table
,我无法从您的问题中判断出来。)
答案 1 :(得分:1)
更改
user_table.userid = ".$_SESSION['userid']." AND approved = 1 OR approved = 2
到
user_table.userid = ".$_SESSION['userid']." AND (approved = 1 OR approved = 2)
在第二个查询中
答案 2 :(得分:0)
SELECT jobs.*, user_table.*
FROM jobs JOIN user_table
ON jobs.userid = user_table.userid WHERE
user_table.userid = ".$_SESSION['userid']." AND (approved = 1 OR approved = 2)