MySQL条件查询语句有哪些?

PHPz
Release: 2020-09-04 15:09:32
Original
4453 people have browsed it

MySQL条件查询语句有哪些?

MySQL 条件查询语句有哪些?

一、 SELECT 条件查询

条件查询

条件查询就是在查询时给出 WHERE 子句, 在WHERE字句中可以使用如下运算符及关键字

==, !=, <>(不等于), <, >, <= >, >=
BETWEEN ... AND
IN(set) : 几个固定值
IS NULL , IS NOT NULL
AND
OR
NOT: 非
Copy after login

二、 OR 条件查询

SELECT * FROM stu WHERE sid = "s_1001" OR sname="lisi";
Copy after login

三、 IN 条件查询

SELECT * FROM stu WHERE sid IN("s_1001", "s_1002", "s_1003");  // 查询符合条件的集合中
+--------+----------+------+--------+
| sid    | sname    | age  | gender |
+--------+----------+------+--------+
| s_1001 | liuYing  |   35 | male   |
| s_1002 | chenger  |   20 | male   |
| s_1003 | zhangsan |   15 | female |
+--------+----------+------+--------+
Copy after login

四、 NOT IN 条件查询

SELECT * FROM stu WHERE sid NOT IN("s_1001", "s_1002", "s_1003");  // 查询符合条件不在集合中
Copy after login

查询结果

+--------+----------+------+--------+
| sid    | sname    | age  | gender |
+--------+----------+------+--------+
| s_1004 | wangwu   |   23 | male   |
| s_1005 | liuYin   |   35 | female |
| s_1006 | zhaoliu  |   70 | male   |
| s_1007 | sunQi    |   15 | female |
| s_1008 | zhouBa   |   43 | male   |
| s_1009 | wuJiu    |   85 | female |
| s_1010 | zhengShi |    5 | male   |
+--------+----------+------+--------+
Copy after login

五、查询是否存在年龄为空 IS NULL

 SELECT * FROM stu WHERE age IS NULL;
Copy after login

六、查询年龄不为空 IS NOT NULL

 SELECT * FROM stu WHERE age IS NOT NULL;
Copy after login

七、使用 BETWEEN AND 或者 >= <= 查询 年龄 20 到 40 之间的数据

  SELECT * FROM stu WHERE age BETWEEN 20 AND 40;
  SELECT * FROM stu WHERE age >= 20 AND age <= 40;
Copy after login

查询结果

+--------+---------+------+--------+
| sid    | sname   | age  | gender |
+--------+---------+------+--------+
| s_1001 | liuYing |   35 | male   |
| s_1002 | chenger |   20 | male   |
| s_1004 | wangwu  |   23 | male   |
| s_1005 | liuYin  |   35 | female |
+--------+---------+------+--------+
Copy after login

更多相关知识,请访问PHP中文网

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!