SQL 选择多个 AND

发布于 2024-11-01 06:29:16 字数 853 浏览 1 评论 0原文

我有一个很长的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

固执像三岁 2024-11-08 06:29:16

使用 IN 关键字,如下所示:

CAT.subcatID IN(5,136)

替换两个 AND。

Use the IN keyword like this:

CAT.subcatID IN(5,136)

to replace your two ANDs.

谁的新欢旧爱 2024-11-08 06:29:16

subcatID 不能同时为 5 和 136,必须使用 OR 而不是 AND。

将其更改为并尝试

ON CAT.busID = R.id AND 
(CAT.subcatID = 5 OR CAT.subcatID = 136) 
AND CAT.deleted = 0";

,或者您可以使用 IN 使其简短

 ON CAT.busID = R.id AND 
 CAT.subcatID IN ('5','136') 
    AND CAT.deleted = 0";

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";
简单气质女生网名 2024-11-08 06:29:16

当您将同一列等于两个不同的值时,不能应用 AND 条件。
使用 OR 条件。

将 $sqlCatC 更改为如下:

$sqlCatC = "INNER JOIN yt_att_data CAT  ON CAT.busID = R.id AND (CAT.subcatID = 5 OR CAT.subcatID = 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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文