简体   繁体   English

MySQL语法查询可一次检查多个值

[英]MySQL syntax query for checking multiple values at once

How do I make this work in MySQL 如何在MySQL中进行这项工作

select (3,5) not in (5,9,8);

where it should return 0 because 5 is in (5,9,8). 它应该返回0,因为5在(5,9,8)中。

Thanks in advance. 提前致谢。

To check if any value in "one column" is present in "another column" of "another table". 检查“另一个表”的“另一个列”中是否存在“一个列”中的任何值。

Using EXISTS 使用EXISTS

SELECT
      one_column
FROM one_table t1
WHERE EXISTS (
      SELECT NULL
      FROM another_table t2
      WHERE t1.one_column = t2.another_column
      )
;

Note in that example above, the subquery select clause does not need to return any values because "checking for existence" occurs in the where clause of that subquery. 注意,在上面的示例中,子查询select子句不需要返回任何值,因为该子查询的where子句中会出现“检查是否存在”。 However a select clause is needed for correct syntax but really it is just decorative here (and so "select 1" or "select *" may be used instead of "select null"). 但是,对于正确的语法,需要选择子句,但实际上它只是装饰性的(因此可以使用“选择1”或“选择*”代替“选择null”)。

Using IN() 使用IN()

SELECT
      one_column
FROM one_table
WHERE one_column IN (
      SELECT another_column
      FROM another_table
      )
;

The select clause in this subquery does have to return values for comparison. 此子查询中的select子句确实必须返回值以进行比较。 Be wary of NULLs being returned if using IN(). 使用IN()时,请小心不要返回NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM