mysql - MySQL - 根据不同的条件忽略某些行
问题描述
例如,我有一Pokemon
列包含某些数据,其中许多重复
口袋妖怪
Wartotle
Pichu
Pikachu
Pichu
Kadabra
Charmelon
Squirtle
Wartotle
Wartotle
Pidgeotto
Pidgeotto
Diglett
我需要的是忽略某些数据,如果相同的特定数据column
不存在。在我通过以下方式获取数据之前,列中的特定数据pokemon
必须存在SELECT
我想要的是一个查询,它做这样的事情,但在多个数据上
SELECT * FROM table
(
If `Pichu` doesn't exists then don't `SELECT` `Pikachu`
If `Abra` doesn't exists then don't `SELECT` `Kadabra`
If `Squirtle` doesn't exists then don't `SELECT` `Wartotle`
If `Pidgety` doesn't exists then don't `SELECT` `Pidgeotto`
If `Squirtle` doesn't exists then don't `SELECT` `Wartotle`
If `Charmander` doesn't exists then don't `SELECT` `Charmeleon`
)
所以SELECT
我需要的查询的最终结果会导致这个
Wartotle
Pichu
Pikachu
Pichu
Squirtle
Wartotle
Wartotle
Diglett
我知道这有点令人困惑,但这是我正在寻找的查询
解决方案
您可以首先找到那些您不想包含的(子查询)并从结果中排除那些(外部查询):
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
应该包含在结果集中。
推荐阅读
- ruby-on-rails - 无法从 Rails 5 中 /layouts/application 中的资产管道声明加载 CSS,在控制器视图中工作正常
- android - Google Maps for Work 的 SDK 更新站点无法正常工作
- apache-spark - 如何在集群外的 Kubernetes 中正确暴露 spark
- emacs - 如何排除文件以执行钩子?
- r - 如何仅获取日期早于具有相同 ID 的另一行的 ID 行?
- authentication - IdentityServer:客户端应用程序使用的正确授权类型是什么?
- file - 写入文件然后立即在 go 中读回
- javascript - Three.js:使用鼠标选择线条对象
- python - 找出导致异常的对象的变量名
- css - Bootstrap 4 HTML - 从视差 jumbotron 中删除圆角