Home > OS >  SQL select where multiple condition by same id from one table
SQL select where multiple condition by same id from one table

Time:11-11

Please help me, I have a table like below and I want to get data by id with status 'sent' and ' ' only and not with status 'delivered'.

message_thread_id status
229
229 delivered
229 sent
229 delivered
229 delivered
240 sent
240 sent
1044
1044
1044
1068 delivered

CodePudding user response:

use NOT EXISTS

SELECT * FROM mytable a WHERE NOT EXISTS(
   SELECT 1 FROM mytable b 
   WHERE a.message_thread_id = b.message_thread_id
   AND status = 'delivered'
)
  • Related