sql基础查询语句

如果对您有帮助的话,帮忙点个赞哦!!!

SQL Joins

1、select * from tableA A inner join tableB B on A.key=B.key

在这里插入图片描述
2、select * from tableA A Left join tableB on A.key=B.key
在这里插入图片描述

3、select * from tableA A right join tableB B on A.key =B.key
在这里插入图片描述

4、select * from tableA A left join tableB B on A.key=B.key where B.key is null
在这里插入图片描述

5、select * from tableA A right join tableB B on A.key = B.key where A.key =B.key where A.key is null
在这里插入图片描述

6、select * from tableA A full join tableB on A.key =B.key
在这里插入图片描述

7、select * from tableA A full outer join tableB B on A.key =B.key where A.key is null or B.key is null
在这里插入图片描述

基本查询语句

1、 使用select语句查询一个数据表
Select * from 表名
2、 查询表中的一列或多列
Select 字段,字段 from 表名
3、 从一个或多个表中获取数据
Select 表1.字段1,表1.字段2, 表2.字段1,表2.字段2,from 表名1,表名2

单表查询

1、 查询所有字段
Select * from 表名
2、 查询指定字段
Select 字段 from表名;
3、 查询指定数据
Select * from 表名 where 字段=“值”
4、 带关键字IN的查询
Select * from 表名 where 条件 IN(元素1,元素2,…);
5、 带Between and的范围查询
Select * from 表名 where 条件 [NOT] between 取值1 And 取值 2;
6、 带LIKE的字符匹配查询
Select * from 表名 where 字段 like ‘%值%’
7、 用关键值IS Null查询空值
Select 字段 from 表名 where 字段 is null;
8、 带关键字and的多条件查询
Select * from 数据表名 where 条件1 and 条件2 [and 条件表达式n]
9、 带关键字OR的多条件查询
Select * from 数据表名 where 条件1 or条件2[or 条件表达式n]
10、用关键字DISTINCT去除结果中的重复行
Select distinct 字段名 from 表名
11、用关键字order by 对查询结果排序
ORDER BY 字段名【ASC|DESC】
Select * from 表名 order by 字段 desc
12、用关键字GROUP BY 分组查询
1、使用关键字group by来分组
Select 字段名 from 表名 group by 字段
2、关键字Group BY 与Group_CONCAT()函数一起使用
Select 字段名 GROUP_CONCAT(字段名)from 表名 GROUP BY 字段名
3、 按多个字段进行分组
Select 字段名 from 表名 group by 字段,字段
13、用关键字limit限制查询结果的数量
Select * from 表名 order by 字段 asc limit n (n表示查询的个数)

聚合函数查询

1、 count()函数
Select count(*) from 表名
2、 sum()函数
Select sum(字段)from 表名
3、 AVG()函数
Select AVG(字段) from 表名
4、 MAX()函数
Select MAX(字段) from 表名
5、 MIN()函数
Select MIN(字段) from 表名

连接查询

1、 内连接查询
Select 字段1,字段2 from 表名1,表名2 where 表名1.字段1=表名2.字段2
2、 外链接查询
Select 字段 from 表名1 left|right join 表名2 on 表名1.字段=表名2.字段
3、 复合条件连接
Select 字段 from 表名1 left|right join 表名2 on 表名1.字段=表名2.字段 and 值>n

子查询

1、带关键字IN的子查询
Select * from 表名 where 字段 IN(select 字段 from 表名)
2、 带比较运算符的子查询(将值进行比较)
Select 字段 from 表名 where 字段>=(select 字段 from 表名)
3、带关键字EXISTS的子查询(判断某个值是否存在)
Select 字段 from 表名 where exists(select 字段 from 表名 where 字段=值)
3、 带关键字ANY的子查询(判断第一个表的值都小于第二个标的值)
Select 字段 from 表名 where 字段<ANY(select 字段 from 表名)
5、带关键字ALL的子查询(判断第一个表的值都大于等于第二个标的值)
Select 字段 from 表名 where 字段>=ALL(select 字段 from 表名)

合并查询

1、 UNION(将2个表进行合并并查询)
Select 字段 from 表名
UNION
Select 字段 from 表名

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值