[英]SQL select where multiple condition by same id from one table with two condition
请帮助我,我有一个如下表,我想通过 id 获取数据,条件为 2:
responded
=> 获取 id where status ='sent'
and status = ''
and status = 'delivered'
pending
=> 获取 id where status ='sent'
和status = 'delivered
,而不是status = ''
message_thread_id | 地位 |
---|---|
229 | |
229 | 发表 |
229 | 发送 |
229 | 发表 |
229 | 发表 |
240 | 发送 |
240 | 发送 |
1044 | |
1044 | |
1044 | |
1068 | 发表 |
这不是这样做的好方法,但却是开始思考的良好开端。
我会在存储过程中的临时表中思考。 但是您的问题没有具体说明,因为您没有任何尝试。
我会制作一个包含多个列的表,指示用户的特定状态是 true 还是 false,填充它然后按查询过滤。
但无论如何,这是“开始思考”查询:
SELECT message_thread_id, 'responded' as status FROM table
WHERE message_thread_id IN (SELECT message_thread_id FROM table WHERE status = 'sent')
AND message_thread_id IN (SELECT message_thread_id FROM table WHERE status = '')
AND message_thread_id IN (SELECT message_thread_id FROM table WHERE status = 'delivered')
UNION ALL
SELECT message_thread_id, 'pending' as status FROM table
WHERE message_thread_id IN (SELECT message_thread_id FROM table WHERE status = 'sent')
AND message_thread_id NOT IN (SELECT message_thread_id FROM table WHERE status = '')
AND message_thread_id IN (SELECT message_thread_id FROM table WHERE status = 'delivered')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.