MySQL学习必备条件查询数据

吾爱主题 阅读:137 2024-04-01 23:23:59 评论:0

一、条件查询

利用where语句可以对数据进行筛选

select * from 表名 where 条件;

 

二、比较运算符

​ 运算符 ​

​ 描述 ​

​ 例子 ​

=

等于

where id = 1

>

大于

where age > 10

<

小于

where age < 10

>=

大于等于

where age >= 10

<=

小于等于

where age <= 10

!=

不等于

where name != '老王'

select * from users where id = 1;

 

三、逻辑运算符

​ 运算符 ​

​ 描述 ​

​ 例子 ​

and

并且

where id = 1 and age > 10

or

或者

where id = 1 or age > 10

not

取反

where not id = 1

select * from users where id = 1 and age = 24;

select * from users where not id = 1;

 

四、范围查询

​ 运算符 ​

​ 描述 ​

​ 例子 ​

in

在指定的非连续范围内

where id in(1,3,5);

between ... and ...

在指定的连续范围内

where id between 1 and 5;

select * from users where id in (1,3,4);

select * from users where id between 1 and 5;

 

五、空判断

​ 运算符 ​

​ 描述 ​

​ 例子 ​

is null

判断是否为空

where name is null

is not null

判断是否不为空

where name is not null

 注:null与''是不一样的

INSERT INTO users (name, birth_date, phone,age)
VALUES ("", "1990-01-01", "13813145213",30);

INSERT INTO users (name, birth_date, phone,age)
VALUES (null, "1990-01-01", "13813145213",30);

INSERT INTO users (name, birth_date, phone,age)
VALUES ("老张", null, "17813145213",30);

select * from users where birth_date is null;

 

六、模糊查询

select * from users where name like "王%";

select * from users where name like "%王";

 

七、优先级

  • 小括号,not,比较运算符,逻辑运算符
  • and比or先运算,如果同时出现并希望先算or,需要结合()使用

 到此这篇关于MySQL学习必备条件查询数据的文章就介绍到这了,更多相关MySQL条件查询数据内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.51cto.com/u_15173612/5052947

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容