Jasir alwafaa
Jasir alwafaa

Reputation: 586

mysql select query php based on column value

i have a table like this,

enter image description here

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

Upvotes: 2

Views: 269

Answers (2)

zakhefron
zakhefron

Reputation: 1443

Try

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

SQL Demo

Upvotes: 1

1000111
1000111

Reputation: 13519

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
)

Upvotes: 2

Related Questions