MySQL的查询语句和多表查询以及子查询

一、查询语句

1、查询一张表

select * from 表名;

2、查询指定字段

select 字段1,字段2,字段3,… from 表名;

3、where条件查询

select 字段1,字段2,字段3 from 表名 where 条件表达式;

4、in关键字查询

select 字段1,字段2 from 表名 where 字段 [not]in(元素1,元素2);

5、between and范围查询

select 字段1,字段2 from 表名 where 字段 [not]between 取值1 and 取值2;

6、like模糊查询

select 字段1,字段2,... from 表名 where 字段 [not]like '字符串';

7、空值查询

select 字段1,字段2,... from 表名 where 字段  is[not] null;

8、and多条件查询

select 字段1,字段2,... from 表名 where 条件表达式1 and 条件表达式2;

9、or多条件查询

select 字段1,字段2,... from 表名 where 条件表达式1 or 条件表达式2;

10、distinct去重复查询

select distinct 字段名 from 表名;

11、对查询结果排序order by

select 字段1,字段2,... from 表名 order by asc/desc;

12、分组查询group by(不能单独使用):

与group_concat()函数一起使用、与聚合函数一起使用、与having一起使用(显示输出的结果)、与with rollup 一起使用(最后加入一个总和行)

group by 属性名 [having 条件表达式][with rollup];

13、limit分页查询

select 字段1,字段2,... from 表名 limit 初始位置,记录数;

二、多表连接查询

select 字段列表 from 表1 inner/left/right join 表2 on 表1.字段 = 表2.字段;

三、子查询(嵌套查询)

子查询:查询多次,多个select语句

注意:第一次的查询结果可以作为第二次的查询的条件或表名使用

子查询中可以包含IN、NOT IN、ANY、ALL、EXISTS、NOT EXISTS等关键字,还可以包含比较运算符= 、 !=、> 、<等

1、作为表名使用

select * from (select * from person) as 表名;

2、any关键字

select ... from ... where a > any(...);

3、all关键字

select ... from ... where a > all(...);

4、some关键字

select ... from ... where a > some(...);

 

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值