四、where子句条件需要的语法
1.使用IN查询多条件
当我们需要查询单个表条件过多时,就会用多个 'OR' 连接或者嵌套,这会比较麻烦,现在我们有 'IN' 能更方便的解决这一问题。
IN 用法:
SELECT *
FROM table_name
WHERE column_name IN 'value';
例子:
假如我们想查询教师表 teachers 中教师国籍为中国 (CN) 或英国 (UK) 的所有教师信息,我们可以使用下面的 SQL 语句:
SELECT *
FROM teachers
WHERE country IN ('CN', 'UK');
执行输出结果:
+----+-----------------+---------------------------+-----+---------+
| id | name | email | age | country |
+----+-----------------+---------------------------+-----+---------+
| 1 | Eastern Heretic | eastern.heretic@gmail.com | 20 | UK |
| 2 | Northern Beggar | northern.beggar@qq.com | 21 | CN |
| 5 | Linghu Chong | NULL | 18 | CN |
+----+-----------------+---------------------------+-----+---------+
2、使用NOT IN排除
'IN' 可以与逻辑运算符 'NOT' 并用,组成 'NOT IN',表示不在集合中的所有结果。
NOT IN 用法:
select *--表的列的所有数据信息
from table_name
where column_name not in 'value'--列的数据信息中没有value的所有数据信息
例子:
假如我们想查询在教师表 teachers 中,国籍 "country" 不为日本 (JP) 和美国 (USA) 的所有教师信息,我们可以使用下面的 SQL 语句:
select *--所有信息
from teachers
where country not in('JP','USA');--国籍 "country" 不为日本 (JP) 和美国 (USA) 的所有教师信息
执行输出结果:
+----+-----------------+---------------------------+-----+---------+
| id | name | email | age | country |
+----+-----------------+---------------------------+-----+---------+
| 1 | Eastern Heretic | eastern.heretic@gmail.com | 20 | UK |
| 2 | Northern Beggar | northern.beggar@qq.com | 21 | CN |
| 5 | Linghu Chong | NULL | 18 | CN |
+----+-----------------+---------------------------+-----+---------+
2211

被折叠的 条评论
为什么被折叠?



