如何在SQL的WHERE子句中将AND运算符与AND运算符一起使用?

时间:2018-08-28 12:38:36

标签: mysql sql phpmyadmin

我正在CodeIgniter模型中使用此数据库查询。

SELECT
                p.product_id,
                p.product_name,
                p.product_photo,
                p.size,
                p.price,
                p.status,
                p.product_image_path
            FROM
                products AS p
            LEFT JOIN
                product_category AS pc
            ON
                p.product_id = pc.product_id
            LEFT JOIN
                vendor_products AS vp
            ON
                vp.product_id = pc.product_id
            WHERE
                pc.category_id = 2
            AND
                vp.vendor_id = 36
            AND 
                pc.subcategory_id IN (1,2)
            AND
                pc.subcategory_value_id IN (1,4)

它返回了我:

It is returning duplicate entries

我只希望那些满足sub_category_value_id所有条件的产品。现在,它返回所有条件。

我是数据库新手,对查询了解不多。

3 个答案:

答案 0 :(得分:0)

似乎您不应该对product_category表使用左联接。 LEFT JOIN关键字返回左表(product_category)中的所有记录,请尝试如下操作:

SELECT
                p.product_id,
                p.product_name,
                p.product_photo,
                p.size,
                p.price,
                p.status,
                p.product_image_path
            FROM
                products AS p
            INNER JOIN
                product_category AS pc
            ON
                p.product_id = pc.product_id
            LEFT JOIN
                vendor_products AS vp
            ON
                vp.product_id = pc.product_id
            WHERE
                pc.category_id = 2
            AND
                vp.vendor_id = 36
            AND 
                pc.subcategory_id IN (1,2)
            AND
                pc.subcategory_value_id IN (1,4)

答案 1 :(得分:0)

您需要group byhaving。您不清楚“所有条件”是什么意思,但是看起来像这样:

SELECT p.*
FROM products p JOIN
     product_category pc
     ON p.product_id = pc.product_id JOIN
     vendor_products AS vp
     ON vp.product_id = pc.product_id
WHERE pc.category_id = 2 AND
      vp.vendor_id = 36 AND
      (pc.subcategory_id, pc.subcategory_value_id) IN ( (1, 1), (2, 4) )
GROUP BY p.product_id  -- this is okay because it is presumably the primary key
HAVING COUNT(DISTINCT pc.subcategory_id) = 2;

注意:

  • WHERE子句将LEFT JOIN变成INNER JOIN,因此请使用正确的JOIN
  • 我假设您要从子类别表中获取一对值。
  • HAVING子句坚持两个子类别都匹配。

答案 2 :(得分:0)

以下查询将根据给定的vendor_id,category_id,subcategory_id和subcategory_value_id为您提供具有product_id和product_name的不同产品。

让我知道这是否适合您!

x.cs
    using References;
....

namespace Name
{
....
public sealed partial class ReaderPage : Page, View.Listener
{
....

private View view;
.....

class MyMenuFlyoutItem : MenuFlyoutItem

   {
    public MyMenuFlyoutItem()
    {
        this.Click += MyMenuFlyoutItem_Click;

    }

private void MyMenuFlyoutItem_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        var dataPackage = new DataPackage();
        dataPackage.SetText(view.vSelGetText());
        Clipboard.SetContent(dataPackage);
    }
   }
....
 }
}


y.cs
....
        public String vSelGetText()
       {
        ....
        }
....