首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mysql从入门到放弃(八)

Mysql从入门到放弃(八)

作者头像
会呼吸的Coder
发布2020-02-17 17:38:07
4710
发布2020-02-17 17:38:07
举报
文章被收录于专栏:会呼吸的Coder会呼吸的Coder

十八、WHERE条件

实例表结构:

+----------+-------------+------+-----+-----------------------+----------------+

| Field | Type | Null | Key | Default | Extra

|+----------+-------------+------+-----+-----------------------+----------------+

| id | int(11) | NO | PRI | NULL | auto_increment || sex | tinyint(4) | NO | | 1 | || username | varchar(20) | YES | | NULL | || age | tinyint(4) | NO | | 18 | || userinfo | varchar(50) | NO | | 我是帅气的lucky老师啊 |

|+----------+-------------+------+-----+-----------------------+----------------+

(1) 比较运算符
  1. > 将id大于5 的性别 更改为0 年龄改为20岁 update user set sex=0,age=20 where id>5;
  2. < 将id小于3 的性别 更改为0 年龄改为23岁 查看id小于4的 性别和用户名的字段数据 select sex,username from user where id<4; update user set sex=0,age=23 where id<3;
  3. >= 删除 id大于等于6的数据 delete from user where id>=6;
  4. <= 查询年龄小于等于23的数据 select * from user where age<=23;
  5. = 查询性别为0的数据 select * from user where sex=0;
  6. !=/<> 查询 用户名不等于lucky的所有数据 select * from user where username!='lucky'; select * from user where username<>'lucky';
(2) 逻辑运算符
  1. and 逻辑与 俩侧为真结果为真 修改年龄为30 id大于1 小于等于2 update user set age=30 where id>1 and id<=2; 查询年龄在18到23之间 不包括本身 select * from user where age>18 and age<23;
  2. or 逻辑或运算 俩侧条件满足一侧就可以 select * from user where age=10 or age=30; select * from user where age>=10 or age<=30;
  3. between and 在...范围之内 包括本身 查询年龄在18~20之间的所有数据
select * from user where age between 18 and 20;

4.等同于


select * from user where age>=18 and age<=20;

5.not between and 不在...之间

查询年龄不在18~20之间的所有数据

select * from user where age not between 18 and 20;

6.等同于

select * from user where age<18 or age>20;

7.in 在...里

查询 年龄在18,20的数据

select * from user where age in(18,20);

8.等同于

select  * from user where age=18 or age=20;

9.not in 不在...里

查询 年龄在18,20的数据

select * from user where age not in(18,20);

等同于

select  * from user where age!=18 and age!=20;
(三) order by 排序 升序/降序

升序

查询数据 按照年龄升序(默认)

   select * from user order by age;
   select * from user order by age asc;

查询数据 按照年龄降序

   select * from user order by age desc;
(4) limit 取值

结构:

limit x 取出x条数据
limit x,y 从x的位置取出y条数据

取出3条数据

 select * from user limit 3;

取出年龄最大/最小的一条数据

select * from user order by age desc limit 1;
select * from user order by age limit 1;

从0开始取出3条数据

select * from user limit 3; 等同于 select * from user limit 0,3;

分页实例:

数据一共100条

每页10条数据

第一页 limit 0,10

第二页 limit 10,10

第三页 limit 20,10

公式:

(nowpage-1)*10
(5) is is not 查询为null的数据

查询username为null的数据

select * from user where username is null;

查询username不为null的数据

select * from user where username is not null;

注意:

因为null是特殊的值 所有不能使用 = 或者 !=进行查询

(6) like 模糊查询
  1. ’%字符‘ 查询以字符结尾的数据 查询以三字为结束的username的数据 select * from user where username like '%三';
  2. '字符%' 查询以字符开头的数据 select * from user where username like '赵%';
  3. '%字符%' 查询包含字符的数据 查询 userinfo中包含lucky的数据 select * from user where userinfo like '%lucky%';
  4. '_' 通配符_ 代表匹配任意一个字符 查询 用户名一个字符的数据 select * from user where username like '_' 查询_lucky的数据 第一位为任意字符 select * from user where username like '_ucky'
  5. not like 查询 用户名除了俩个字符以外的任意数据 select * from user where username not like '__';
  6. [charlist] 原子表 原子表内的任意一个字 从上面的 "Persons" 表中选取居住的城市以 "A" 或 "L" 或 "N" 开头的人: SELECT * FROM Persons WHERE City LIKE '[ALN]%'
‍(7) DISTINCT 去除重复的数据
 select distinct userinfo from user;
select distinct 字段名  from 表名;
(8) 子查询 (查询的条件还是一条SQL语句)
select  * from 表名 where 字段名 in(SQL语句)

实例:

select * from user where age in(select age from user where sex=0);
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-09-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 初级程序员 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 十八、WHERE条件
    • (1) 比较运算符
      • (2) 逻辑运算符
        • (三) order by 排序 升序/降序
          • (4) limit 取值
            • (5) is is not 查询为null的数据
              • (6) like 模糊查询
                • ‍(7) DISTINCT 去除重复的数据
                  • (8) 子查询 (查询的条件还是一条SQL语句)
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档