[英]Select multiple rows that correspond to a single condition
I have a table where POST_ID contains an ID
(that identifies a post), META_KEY contains the type of data (“ latitude ” or “ longitude ”) and META_VALUE contains the value for the key. 我有一个表,其中POST_ID包含一个ID
(用于标识帖子), META_KEY包含数据的类型(“ 纬度 ”或“ 经度 ”),而META_VALUE包含键的值。
POST_ID META_KEY META_VALUE
1879 latitude 14.846329
1879 longitude 42.947395
1849 latitude 18.543265
1849 longitude 41.849382
1754 latitude 14.846329
1754 longitude 41.849382
In simple English, I need to know the ID
of the post where latitude is 14.846329 and longitude is 42.947395 (in this case 1879 ) 用简单的英语,我需要知道帖子的ID
,其中纬度为14.846329 , 经度为42.947395 (在这种情况下为1879 )
I am totally confused at the moment: what's the MySQL
query to get the correct result? 此刻我很困惑: 要获取正确结果的MySQL
查询是什么?
Thanks. 谢谢。
Group by the post_id
and take only those having these values 按post_id
分组,仅采用具有这些值的值
select post_id
from your_table
group by post_id
having sum(meta_key = 'latitude' and meta_value = '14.846329') > 0
and sum(meta_key = 'longitude' and meta_value = '42.947395') > 0
One method uses a join
: 一种方法使用join
:
select t.post_id
from t tlat join
t tlong
on tlat.post_id = tlong.post_id and
tlat.meta_key = 'latitude' and tlat.meta_value = '14.846329'
tlong.meta_key = 'longitude' and long.meta_value = '42.947395';
Note: This uses single quotes, assuming that the meta_value
is a string. 注意:假设meta_value
是字符串,则使用单引号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.