MySql查询

我所使用的表的结构

idnameagehighgenderclsbirthis_delete
1iron man28179.9912020-01-011
3shell20212.221NULL0
4joy20160.0011996-01-010
5jujuboy18141.0012019-11-290
6fafa18179.9922019-11-290
7yao40212.0022019-11-290
8jr25202.0022019-11-290
9shell20186.00保密32019-11-290
10jr25182.00保密32019-11-290
11iron man28199.0032019-11-290
12lingo25173.0042019-11-290

MySql查询

select 基础语法

select * from 表名字;

select 完整语法

select 去重选项 字段列表 [as 字段别名] from 数据源 [where子句] [group by 子句] [having 子句] [order by 子句] [limit子句];

简单的用法参照上一遍文章

条件查询

使用wheree子句对表中的数据筛选,结果为True的行会出现在结果集中

语法

select * from 表名 where 条件;
select name form students where id = 1;

比较运算符

  • 等于=
  • 大于>
  • 大于等于>=
  • 小于<
  • 小于等于<=
  • 不等于!= or <>
select * from students where id = 6;
select * from students where age>=20;

逻辑运算符

  • and
  • or
  • not

运算符优先级 not > and > or

select * from students where age<20 and high<180;
select * from students where age=20 or gender=2;
-- 查询18到25之间的所有学生信息
select * from students where age>18 and age<25;
-- 查询编号小于4或没被删除的学生姓名
select name from students where id<4 or is_delete=0;
-- 查询年龄不是18且不是女性的学生姓名,年龄,性别
select name,age,gender from students where not (age=18 and gender=2);

模糊查询

  • like
  • %表示任意个字符串
  • _表示一个字符串
-- 查询姓名中 以 "s" 开始的名字 
select name from students where name like 's%';
-- 查询姓名中 有 "o" 所有的名字
select name from students where name like '%o%';
-- 查询有2个字的名字
select name from students where name like '__';
-- 查询至少有5个字的名字
select name from students where name like '_____%';

  • 正则表达式
. :匹配任意单个字符串
* :匹配0个或多个前一个得到的字符
[] :匹配任意一个[]内的字符,[ab]*可以匹配空字符穿、a,b、或由任意个a和b组成的字符串。
^ :匹配开头,如^s匹配以s开头的字符串
# :匹配结尾,如s$匹配以s结尾的字符串
{n} :匹配前一个字符反复n次

rlike or regexp

匹配姓名以i开头,以n结尾的字符串(表中有iron man)的信息

select * from students where name relike '^i.*n$';
select * from students where name regexp '^i.n$';
  • 范围查找
-- in(1,3,8)表示在一个非连续的范围内查找
select * from students where age in (18,40);
select * from students where name in ('shell','iron man');
-- between...and...表示在一个连续的范围内查找
select * from students where high between 180 and 200;
select * from students where (age between 18 and 22) and gender=2;
-- 空判断is null
select * from students where birth is null;
select * from students where birth is not null;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值