在MYSQL中,针对同一列指定多次“AND”条件。

3

我有一个类似于下面的表:

product_id | attribute_id | text
--------------------------------
52         | 16           | 1.0 Inch - 2.9 Inches
52         | 15           | Radio
52         | 14           | Simple Phones
134        | 16           | 1.0 Inch - 2.9 Inches
134        | 15           | Wifi
134        | 14           | Dual SIM Phones

我是为获取屏幕大小在1.0英寸至2.9英寸之间的简单手机产品编写查询。

我想在一个列上放置两个条件。

当我编写查询时:

select
      * 
from
      product_attribute 
where 
      (text = '1.0 Inch - 2.9 Inches') 
      and 
      (text = 'Simple Phones')

我运行以下查询时,出现“0结果”:

select
      * 
from
      product_attribute 
where 
      text IN('1.0 Inch - 2.9 Inches','Simple Phones')

然后我得到了以下结果:

product_id | attribute_id | text
--------------------------------
52         | 16           | 1.0 Inch - 2.9 Inches
52         | 14           | Simple Phones
134        | 16           | 1.0 Inch - 2.9 Inches

但是我只需要product_id = 52,因为这个产品具有1.0英寸-2.9英寸和简单手机两个过滤器,而product_id = 134只有1.0英寸-2.9英寸。

请帮我解决这个问题。

** 抱歉我的英语不好 :)

6个回答

2

使用 HAVING 子句:

select *
from product_attribute 
where text IN('1.0 Inch - 2.9 Inches','Simple Phones')
group by product_id
having count(product_id)=2

请参考SQL Fiddle中的示例。

编辑:

要获取所有记录:

select *
from product_attribute T1 LEFT JOIN
  (select product_id
   from product_attribute 
   where text IN('1.0 Inch - 2.9 Inches','Simple Phones')
   group by product_id
   having count(product_id)=2) T2 on T1.product_id=T2.product_id
WHERE T2.product_id IS NOT NULL
AND T1.text IN('1.0 Inch - 2.9 Inches','Simple Phones')

结果:

PRODUCT_ID    ATTRIBUTE_ID  TEXT
52            16            1.0 Inch - 2.9 Inches
52            14            Simple Phones

SQL Fiddle中查看结果。


@Asad:我编辑了我的答案。现在试试看。请查看fiddle。 - Raging Bull
是的,它满足了我的需求 :) 感谢 @Raging Bull - Asad Nadeem

1
每一行都被视为单独的实体,因此即使它们具有相同的product_id,对于product_id 52的2个匹配条目也会被视为不同。最好按product_id分组行,然后应用in条件。
例如。 SELECT id,com_string from(SELECT id,GROUP_CONCAT(string SEPARATOR' ')作为com_string FROM table GROUP BY id)temp where com_string like('%1.0 Inch - 2.9 Inches%')and com_string like('%Simple Phones%');

SELECT product_id, com_string from (SELECT product_id, GROUP_CONCAT(text, ' ') as com_string FROM product_attribute GROUP BY product_id) temp where com_string like ('%1.0英寸 - 2.9英寸%') and com_string like ('%简单手机%'); - Saurabh Mishra

1
您的查询非常复杂但是可行:

select * 
from product_attribute 
where product_id in 
(
    select t.filtered_id, t.joined_text from
    ( -- selecting all rows with 
      -- '1.0 Inch - 2.9 Inches','Simple Phones' and concatenate
       select
          product_id as filtered_id, group_concat(`text`) as joined_text
       from
          product_attribute 
       where 
          text IN('1.0 Inch - 2.9 Inches','Simple Phones')
       group by product_id ) as t

    where   -- mysql may concatenate in any order
      t.joined_text = '1.0 Inch - 2.9 Inches,Simple Phones'
      or 
      t.joined_text = 'Simple Phones,1.0 Inch - 2.9 Inches'
)

1

你需要执行SELF JOIN

SELECT *
FROM product_attribute AS t1 INNER JOIN product_attribute AS t2
    ON t1.product_id = t2.product_id
WHERE t1.text = '1.0 Inch - 2.9 Inches' AND t2.text = 'Simple Phones'

其他人的答案应该返回相同的结果,但性能不同。请尝试所有答案并接受最佳答案。


1

试试这个

SELECT *
FROM product_attribute
WHERE product_id in
    (SELECT product_id
     FROM product_attribute
     WHERE text IN('1.0 Inch - 2.9 Inches','Simple Phones')
     GROUP BY product_id HAVING count(product_id)=2);

0
在这种特定情况下,我会使用自连接:
SELECT pa.*
FROM product_attribute pa,
  product_attribute pa1,
  product_attribute pa2
WHERE pa.product_id = pa1.product_id
AND pa.product_id   = pa2.product_id
AND pa1.text        = '1.0 Inch - 2.9 Inches'
AND pa2.text        = 'Simple Phones';

这种方法的优点是,您可以动态地为相同的列添加条件。我相信这很容易理解。我没有在我的机器上测试过这个查询,但我相信它会对您有用。

谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接