选择多个列和行的条件

时间:2019-03-15 12:36:22

标签: mysql sql

我该如何选择所有具有upload_key的视频 =主机,cnn在upload_value中,并发送= 0

+----------+---------+------------+--------------+
| video_id | meta_id | upload_key | upload_value |
+----------+---------+------------+--------------+
| 1        | 6       | host       | cnn          |
| 1        | 7       | send       | 0            |
+----------+---------+------------+--------------+

2 个答案:

答案 0 :(得分:1)

您可以在下面尝试-

DEMO

select video_id
from tablename where (upload_key,upload_Value) =('host','cnn')
   and exists (select 1 from tablename where (upload_key,upload_Value)=('send','0'))

答案 1 :(得分:1)

  

选择所有在upload_value中具有upload_key =主机且cnn的视频,并发送= 0

一种方法是聚合和having

select video_id
from t
where upload_key in ('host', 'send')
group by video_id
having sum( upload_key = 'host' and upload_value = 'cnn' ) > 0 and
       sum( upload_key = 'send' and upload_value = '0' ) > 0;