选择具有给定条件的数据MySQL

时间:2017-04-26 03:22:37

标签: mysql database select

让我们快速解决问题,下表更好地描述了这个问题:

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

现在,我希望输出是ABC,BCD和FGH,因为我的给定条件是选择批准值不是4的每个名称.CDE,DEF和EFG有4个值,所以它们没有被选中。当然,我仍然不知道如何构建这个条件,所以任何帮助都会受到赞赏。

更新

我想要一个ID号最高的行,所以结果是ID为2的ABC,ID为5的BCD,ID为14的FGH。如何解决?

2 个答案:

答案 0 :(得分:1)

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

至于“更新”:

        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

答案 1 :(得分:1)

select distinct name from myTable where approved != 4;