如何从表中选择包含一组重复值(clm1,clm2,cl3)的id?

时间:2015-04-29 02:50:22

标签: php mysql sql

如何选择包含重复值(product_id,place_id,product_type_id)的id = 32和28? 请帮我高级谢谢..

id        product_id   place_id    product_type_id  quaty       year      month
22           3           2              1            10         2015       4          
28           6           1              3            10         2015       4          
32           6           1              3            15         2015       5          
33           11          3              6            100        2015       5          
27           30          1              3            13         2015       4   

3 个答案:

答案 0 :(得分:3)

试试这个:

checkbox=[]

这会给你:

SELECT id
FROM table1 AS t1
WHERE product_id IN(SELECT product_id
                    FROM table1 AS t2
                    GROUP BY product_id,place_id,product_type_id
                    HAVING COUNT(id) > 1);

答案 1 :(得分:0)

使用not exists

SELECT t.*
FROM table1 t
WHERE NOT EXISTS (SELECT 1
                  FROM table1 t1
                  WHERE t.product_id = t1.product_id AND t.place_id = t1.place_id AND
                        t.product_type_id = t1.product_type_id
                        t.id <> t1.id
                 )

虽然您可以使用count(*)执行类似操作,但此查询的性能应该更好。此外,它可以利用table(product_id, place_id, product_type_id)上的索引。

答案 2 :(得分:-2)

我希望这会有所帮助。

SELECT Table1.ID FROM FROM <Table1>
INNER JOIN
(SELECT product_ID, place_id, product_type_id
FROM <Table1> 
Group BY product_ID, place_id, product_type_id
HAVING COUNT(ID) > 1 )a
ON Table1.product_ID = a.product_ID
AND Table1.place_id = a.place_id
AND Table1.product_type_id = a.product_type_id