[英]is there a better way to write this mysql select query?
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不等于会话userid的记录。
您可以尝试IN语法:
WHERE user_table.userid = ".$_SESSION['userid']."
AND approved IN (1,2)
如果您在usertable(userid, approved)
上创建复合索引, usertable(userid, approved)
该查询将利用该索引(如果approved
在user_table
approved
,则无法从您的问题中得知)。
更改
user_table.userid = ".$_SESSION['userid']." AND approved = 1 OR approved = 2
至
user_table.userid = ".$_SESSION['userid']." AND (approved = 1 OR approved = 2)
在第二个查询中
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.