programing

mysql/mariadb에서 ID가 같은 여러 행을 WHERE로 쿼리하는 방법

codeshow 2023. 9. 24. 13:12
반응형

mysql/mariadb에서 ID가 같은 여러 행을 WHERE로 쿼리하는 방법

저는 꼼짝도 못하고 적절한 WHERE 쿼리를 수행하는 방법을 알아내려고 노력하고 있습니다.다음 표입니다.

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 

게시물을 선택하고 싶습니다, WHEREproperty_value_id = 3그리고.property_value_custom = "111".

결과는 post_id 79이어야 합니다.어떻게 질문하죠?

넌 할 수 있다.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

또는:

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

언급URL : https://stackoverflow.com/questions/63567435/how-to-where-query-muliple-rows-with-the-same-id-as-one-in-mysql-mariadb

반응형