简体   繁体   English

MYSQL一个复杂的查询选择语句

[英]MYSQL A complicated query select statement

Click this to view image 点击查看图片

I have 3 products. 我有3个产品。 76,77,78. 76,77,78。 I want to return rows which have have distinct specifications_id but ALL product must have that specifications_id. 我想返回具有不同的specification_id的行,但所有产品都必须具有该specification_id。

For example: In the image above, 76, 77, 78 each have specifications_id of 1. I would want to return that row. 例如:在上图中,每个76、77、78的specification_id为1。我想返回该行。

But in the example of product_id is 76, where it has a specifications_id of 2, the other two products (77,78) do not have a specifications_id = 2. 但是在product_id的示例为76的情况下,其specification_id为2,其他两个产品(77,78)的specs_id = 2。

I know how to do it using a series of loops ect, but I'm curious if there's a shorter way in mysql. 我知道如何使用一系列循环等操作,但是我很好奇mysql中是否有更短的方法。

Say this table had several rows and I wanted all the products which had the same specifications_id in all 3 products. 假设此表有几行,我希望所有3种产品中具有相同specs_id的所有产品。

If I understood your question correctly, this should do it: 如果我正确理解了您的问题,则应该这样做:

select * from myTable where specifications_id in
( 
   select specifications_id from myTable 
   having count(specifications_id) > 2
 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM