简体   繁体   中英

mysql select query php based on column value

i have a table like this,

在此处输入图片说明

i want to select all doc no having status 1. select * from tbl where doc_status=1 will select 101,102,101 . But in for doc_no 101 there is doc-status 0 also so it wont select.How can i do that, desired output will be 103 only

You can use NOT EXISTS

SELECT 
DISTINCT T.doc_no
FROM tbl T
WHERE NOT EXISTS 
(
 SELECT 
 1
 FROM tbl TT
 WHERE TT.doc_status = 0
 AND TT.doc_no = T.doc_no
)

Working Demo

Note: I guess doc_status can hold value either 0/1 . If this is the case then no need to filter with doc_status = 1 outside.

OR you can use NOT IN

SELECT 
 DISTINCT T.doc_no
FROM tbl T
WHERE T.doc_no NOT IN
(
 SELECT 
    TT.doc_no
 FROM tbl TT
 WHERE TT.doc_status = 0
)

Try

SELECT * FROM test GROUP BY doc_no HAVING MIN(doc_status) = 1;

SQL Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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