简体   繁体   English

如何在 mysql/mariadb 中查询与 id 相同的多行

[英]How to WHERE query muliple rows with the same id as one in mysql/mariadb

i'm stuck and trying to figure out how to do the right WHERE query.我被困住了,试图弄清楚如何做正确的 WHERE 查询。 This is the table:这是表:

post_id   property_id   property_value_id   property_value_custom   
77        2             3                   NULL
79        1             1                   NULL
79        2             2                   NULL
79        2             3                   NULL
79        4             NULL                111
80        3             4                   NULL 

I want to select the post, WHERE property_value_id = 3 AND property_value_custom = "111" .我想选择帖子, WHERE property_value_id = 3 AND property_value_custom = "111"

The result should be post_id 79. How to query that?结果应该是 post_id 79。如何查询?

You can group by post_id and set the condition in the HAVING clause:您可以group by post_id并在HAVING子句中设置条件:

select post_id
from tablename
group by post_id
having sum(property_value_id = 3) > 0 
   and sum(property_value_custom = 111) > 0

or:或者:

having max(property_value_id = 3) = 1 
   and max(property_value_custom = 111) = 1

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

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