且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL - 根据不同的条件忽略某些行

更新时间:2023-01-24 16:38:19

你可以先找到那些你不想包含的(子查询),然后从结果中排除那些(外查询):

You could first find those that you do not want to include (subquery) and exclude those from the result (outer query):

select t.name
from table1 t
where t.name not in (
  select s.skip
  from (
    select 'Pichu' as search, 'Pikachu' as 'skip'
    union
    select 'Abra', 'Kadabra'
    union
    select 'Squirtle', 'Wartotle'
    union
    select 'Pidgety', 'Pidgeotto'
    union
    select 'Squirtle', 'Wartotle'
    union
    select 'Charmander', 'Charmeleon'
  ) as s 
    left join table1 t1 on t1.name=s.search
  where t1.name is null
);

顺便说一句,根据您的规则,Charmelon 应该包含在结果集中.

Btw, based on your rules, the Charmelon should be included in the result set.

参见 dbfiddle