Rayan Suryadikara
Rayan Suryadikara

Reputation: 247

Select Data With Given Conditiion MySQL

Let's get quick on the problem, which is better described with this table below :

ID   Name         Approved
-----------------------------
1    ABC          1
2    ABC          2
3    BCD          1
4    BCD          2
5    BCD          3
6    CDE          1
7    CDE          2
8    CDE          3
9    CDE          4
10   DEF          1
11   DEF          4
12   EFG          4
13   FGH          2
14   FGH          3

Now, I want the outputs are ABC, BCD and FGH, because my given condition is selecting every name which approved value is not 4. CDE, DEF, and EFG has a 4 value, so they're not selected. Of course, I still don't know how to construct this condition, so any help appreciated.

UPDATE

I want to take a row with the highest ID number, so the result is ABC with ID 2, BCD with ID 5, and FGH with ID 14. How to solve it?

Upvotes: 0

Views: 30

Answers (2)

user5328504
user5328504

Reputation: 764

    select distinct name from mytable x 
where not exists (select 1 from mytable y where x.name = y.name and approved = 4)

as for "the update":

        select name, MAX(approved) from mytable x 
    where not exists (select 1 from mytable y where x.name = y.name and approved = 4) 
group by name order by  MAX(approved) DESC

Upvotes: 1

user7427848
user7427848

Reputation:

select distinct name from myTable where approved != 4;

Upvotes: 1

Related Questions