SQL Select With Multiple And
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 =开发者_StackOverflow社区 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.
Use the IN
keyword like this:
CAT.subcatID IN(5,136)
to replace your two ANDs.
The subcatID can not be 5 and 136 at a time you have to use OR instead of AND.
Change it to and try
ON CAT.busID = R.id AND
(CAT.subcatID = 5 OR CAT.subcatID = 136)
AND CAT.deleted = 0";
OR you can make it short using IN
ON CAT.busID = R.id AND
CAT.subcatID IN ('5','136')
AND CAT.deleted = 0";
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:
$sqlCatC = "INNER JOIN yt_att_data CAT ON CAT.busID = R.id AND (CAT.subcatID = 5 OR CAT.subcatID = 136) AND CAT.deleted = 0";
精彩评论