MySQL数据库WHERE条件等相关操作

WHERE条件

1、表结构

CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(10) DEFAULT 'xxx',
  `sex` enum('w','m') DEFAULT 'w',
  `age` tinyint(4) DEFAULT '18',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8

2、比较运算符

  • >

    查询年龄大于20岁的

    select * from user where age>20;

    将年龄大于20的性别改为w 年龄减少2

    update user set sex='w', age=age-2 where age > 20;

  • <

    查询年龄小于20岁的

    select * from user where age<20;

    将年龄小于20的性别改为w 年龄减少2

    update user set sex='w', age=age-2 where age < 20;

  • >=

    删除年龄大于等于30的数据

    delete from user where age >= 30;

  • <=

    删除id 小于等于 5 的数据

    delete from user where id <= 5;

  • =

    查找性别为w的数据

    select * from user where sex='w';

  • !=/<>

    查询性别不为w的所有数据

    select * from user where sex != 'w';

    select * from user where sex <> 'w';

3、逻辑运算符

  • and 逻辑与 两侧为真才为真

    查询年龄为18 并且性别为w的数据

    select * from user where sex='w' and age=18;

    update user set username='zhangsan' where sex='w' and age=18;

  • or 逻辑或 满足一个条件就可以

    修改性别为w或者m的数据 将年龄+2岁

    update user set age=age+2 where sex='w' or sex='m';

  • between ... and ... 在...之间 包括本身

    查询年龄在18 - 28之间的数据

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

    select * from user where age between 18 and 28;

  • not between and 不在...之间

    查询年龄不在18 - 28之间的数据

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

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

  • in 在...里

    查询年龄为 18 28 38 的数据

    select * from user where age=18 or age=28 or age=38;

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

  • not in 不在...里

    查询年龄不为 18 28 38 的数据

    select * from user where age!=18 and age!=28 and age!=38;

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

4、order by 排序

  • 升序 asc

    查询年龄从小到大

    select * from user order by age;

    select * from user order by age asc;

  • 降序 desc

    查询年龄从大到小

    select * from user order by age desc;

    select * from user where sex='w' order by age;

5、limit 取值

  • 结构

    limit x 取出x条数据

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

  • 使用

    取出3条数据

    select * from user limit 3;

    从第3条开始 取出3条

    select * from user limit 3,3;

    取出年龄最大的数据

    select * from user order by age desc limit 1;

    取出年龄最小的数据

    select * from user order by age asc limit 1;

    查询性别为w的 最大的年龄的数据

    select * from user where sex='w' order by age desc limit 1;

  • 分页

    100 条数据

    1页显示10条

    1 页数据 limit 0,10

    2 页数据 limit 10, 10

    limit 规律

    (nowpage -1)*everyPage

6、is is not 对于null值进行判断

  • is

    查询为空的数据

    select * from user where username is null;

  • is not

    查询不为空的数据

    select * from user where username is not null;

注意:

因为空值为特殊的值 所以不能使用= != 等进行判断

7、like模糊查询

  • '%字符' 查询以某个字符结尾的数据

    查询用户名以y结尾的数据

    select * from user where username like '%y';

  • '字符%'查询以某个字符作为开头的数据

    查询用户名以l开头的数据

    select * from user where username like 'l%';

  • '%字符%' 包含关系

    查询包含y的用户名

    select * from user where username like '%y%'

  • '_' 通配符匹配任意一位字符的数据

    匹配俩位的用户名

    select * from user where username like '__';

  • not like 查询匹配到的以外的数据

    查询俩个字符以外的数据

    select * from user where username not like '__';

8、MySQL正则表达式

mysql页同样支持正则表达式下面的符号用于正则regexp操作符中

模式 描述
^ 以某个字符作为开始
$ 以某个字符作为结尾
. 匹配任意一个字符 \n以外
[] 字符集合 任意一个字符 [abc]
[^...] 取反
|
* 匹配零次到多次
+ 匹配前面正则表达式的1次到多次
匹配前面表达式的n次
匹配前面表达式的n~m次

查询username中以l作为开头的数据

select * from user where username regexp '^l';

查询username中以y作为结尾的数据

select * from user where username regexp 'y$';

查询username中包含y的数据

select * from user where username regexp 'y'

查询为俩个字符的数据

select * from user where username regexp '[1]{2}$';

查询字母以外的用户名

select * from user where username regexp '[^a-z]';


  1. a-z ↩︎

posted @ 2022-03-16 11:08  寻月隐君  阅读(279)  评论(0编辑  收藏  举报