Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 有没有一种方法可以;捕获;IN()语句中所有跳过的项?_Mysql_Sql - Fatal编程技术网

Mysql 有没有一种方法可以;捕获;IN()语句中所有跳过的项?

Mysql 有没有一种方法可以;捕获;IN()语句中所有跳过的项?,mysql,sql,Mysql,Sql,我有一张这样的桌子: myTable ------------------- |Column1 | Column2 | |--------+----------| |foo |bla | |bar |blabla | |baz |blablabla | |qwe |blabal | --------+---------- 现在,为了搜索多个条目,我正在使用SELECT*FROM myTable,其中第1列位于('foo'、'bar

我有一张这样的桌子:

 myTable
 -------------------
|Column1 | Column2  |
|--------+----------|
|foo     |bla       |
|bar     |blabla    |
|baz     |blablabla |
|qwe     |blabal    |
 --------+----------
现在,为了搜索多个条目,我正在使用
SELECT*FROM myTable,其中第1列位于('foo'、'bar'、'tre'、'gaz'、'hed'、…)
。但是,这只显示表中存在的所有项,并跳过所有与表中任何项不匹配的项

 result:
 -------------------
|Column1 | Column2  |
|--------+----------|
|foo     |bla       |
|bar     |blabla    |
 --------+----------

SQL中是否有方法捕获
“tre”、“gaz”、“hed”…
以及表中不存在但在
in()
子句中的所有项???

您可以使用
不存在的子查询,如下所示:

select t.col
  from (select 'foo' as col
       union select 'bar'
       union select 'tre'
       union select 'gaz'
       union select 'hed') t
  where not exists (select 1 from myTable m WHERE Column1 = t.col)

您可以将
不存在
与子查询一起使用,如下所示:

select t.col
  from (select 'foo' as col
       union select 'bar'
       union select 'tre'
       union select 'gaz'
       union select 'hed') t
  where not exists (select 1 from myTable m WHERE Column1 = t.col)

您可以创建一个包含所有所需行的虚拟表并联接此表。虚拟表、联合子查询或CTE-取决于使用的版本。如果要从列表中接收值,则列表必须是行集。中的列表不存在。使用子查询(请参见Popeye的答案)或将值保存到临时表中。您可以创建一个包含所有所需行的虚拟表并联接此表。虚拟表、联合子查询或CTE-取决于使用的版本。如果要从列表中接收值,则列表必须是行集。中的列表不存在。要么使用子查询(参见Popeye的答案),要么将值保存到临时表中。