SQL 选择多个 AND
我有一个很长的 sql 语句,我试图添加一些 AND 条件,但它不起作用。
这是我的声明:
if($cat=="att") { $sqlCatC = "INNER JOIN yt_att_data CAT
ON CAT.busID = R.id AND CAT.subcatID = 5 AND CAT.subcatID = 136 AND CAT.deleted = 0"; }
//Get RegionInfo busID based on region, approved
$sql= "SELECT DISTINCT CAT.busID,R.id,companyName,membershipID,addressID,city,logo,descriptionShort FROM yt_Business_RegInfo R
$sqlReg
INNER JOIN yt_Business_Seasons S
ON R.id = S.busID AND S.deleted = 0
INNER JOIN yt_Business_Address A
ON R.addressID = A.id
INNER JOIN yt_Business_Membership M
ON R.membershipID = M.id AND M.approved = 1
$sqlCatC
WHERE R.deleted = 0
ORDER BY R.companyName ASC LIMIT $start, $limit";
在顶部 $sqlCatC 中,我尝试添加一个 AND 条件,其中 CAT.subcatID 等于 5 和 136。它适用于检查单个 CAT.subcatID,但不适用于两个。任何建议将不胜感激。
I've got a very long sql statement that I'm trying to add some AND conditions to but it's not working.
Here is my statement:
if($cat=="att") { $sqlCatC = "INNER JOIN yt_att_data CAT
ON CAT.busID = R.id AND CAT.subcatID = 5 AND CAT.subcatID = 136 AND CAT.deleted = 0"; }
//Get RegionInfo busID based on region, approved
$sql= "SELECT DISTINCT CAT.busID,R.id,companyName,membershipID,addressID,city,logo,descriptionShort FROM yt_Business_RegInfo R
$sqlReg
INNER JOIN yt_Business_Seasons S
ON R.id = S.busID AND S.deleted = 0
INNER JOIN yt_Business_Address A
ON R.addressID = A.id
INNER JOIN yt_Business_Membership M
ON R.membershipID = M.id AND M.approved = 1
$sqlCatC
WHERE R.deleted = 0
ORDER BY R.companyName ASC LIMIT $start, $limit";
It's in the top $sqlCatC that I'm trying to add an AND condition of where CAT.subcatID is equal to 5 and 136. It works with checking for a single CAT.subcatID but not for two. Any suggestions would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
IN
关键字,如下所示:替换两个 AND。
Use the
IN
keyword like this:to replace your two ANDs.
subcatID 不能同时为 5 和 136,必须使用 OR 而不是 AND。
将其更改为并尝试
,或者您可以使用 IN 使其简短
The subcatID can not be 5 and 136 at a time you have to use OR instead of AND.
Change it to and try
OR you can make it short using IN
当您将同一列等于两个不同的值时,不能应用 AND 条件。
使用 OR 条件。
将 $sqlCatC 更改为如下:
You cannot have an AND condition applied when you are equating the same column to two different values.
Use an OR condition.
Change the $sqlCatC to as below: