首页 > 解决方案 > 如何消除mysql中的特定行

问题描述

id  manager_id   name

1      1         Tony
2      2         smith
3      2         harry
4      1         jack
5      1         william
6      2         steve
7      2         no name
8      2         john
9      2         no name

我使用此查询来获取相同的最后订购 id。

select t.* 
  from tablename t 
 where not exists (select 1 
                     from tablename 
                    where manager_id <> t.manager_id 
                      and id > t.id)
| id  | manager_id | name    |
| --- | ---------- | -----   |
| 6   | 2          | steve   |
| 7   | 2          | no name |
| 8   | 2          | john    |
| 9   | 2          | no name |

上面的查询工作正常。但现在我需要消除 name = no name 的特定行。那么如何自定义查询。

标签: mysqlsql

解决方案


请使用以下查询来消除无名,

select t.* 
from tablename t 
where not exists (select 1 
                 from tablename 
                where manager_id <> t.manager_id 
                  and id > t.id) and t.name != 'no name';

推荐阅读