简体   繁体   English

选择具有相同id但不同值的不同行

[英]Selecting different rows with same id but different values

在此处输入图片说明

I have a table above, I want to get the record_id(3 in this case) having meta_name=discount_percent and meta_value=56 combined with another condition meta_name=discount_multiplier and meta_value=30.我有一个上面的表格,我想获得具有 meta_name=discount_percent 和 meta_value=56 以及另一个条件 meta_name=discount_multiplier 和 meta_value=30 的 record_id(在这种情况下为 3)。 I tried using HAVING clause with GROUP BY but it didn't work.我尝试将HAVING子句与GROUP BY一起使用,但没有用。 Would appreciate if someone can help.如果有人可以提供帮助,将不胜感激。

I believe you want to get the records with your two conditions group them by the ID and check if the count of rows in the group is 2, so that both conditions applied to that ID (assuming that the pair of record_id and meta_name is unique).我相信您希望获得具有两个条件的记录,并按 ID 对它们进行分组,并检查组中的行数是否为 2,以便两个条件都应用于该 ID(假设record_idmeta_name是唯一的) .

SELECT record_id
       FROM elbat
       WHERE meta_name = 'discount_percent'
             AND meta_value = '56'
              OR meta_name = 'discount_multiplier'
                 AND meta_value = '30'
       GROUP BY record_id
       HAVING count(*) = 2;

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

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