阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL基本命令-SQL语句

163次阅读
没有评论

共计 2402 个字符,预计需要花费 7 分钟才能阅读完成。

服务端命令 SQL

在数据库系统中,SQL 语句不区分大小写(建议用大写)
SQL 语句可单行或多行书写,以“;”结尾
关键词不能跨多行或简写
用空格和缩进来提高语句的可读性
子句通常位于独立行,便于编辑,提高可读性
注释:
SQL 标准:
/* 注释内容 */ 多行注释
— 注释内容 单行注释,注意有空格
MySQL 注释:
#

SQL 优化

查询时,能不要 * 就不用 *,尽量写全字段名
大部分情况连接效率远大于子查询
多表连接时,尽量小表驱动大表,即小表 join 大表
在千万级分页时使用 limit
对于经常使用的查询,可以开启缓存
多使用 explain 和 profile 分析查询语句
查看慢查询日志,找出执行时间长的 sql 语句优化

sql 查询:单表查询和多表查询

两张表合并:横向合并、纵向合并

纵向合并:两张表挑出相同的字段进行合并(注意顺序)

范例

SQL 查询范例

1、给表的字段名添加别名
select stuid as 学生编号,name 姓名,gender 性别 from students;
2、查询年龄大于 40 的
select * from students where age >40;
3、查找年龄大于 20 小于 40 的
select * from students where age < 40 and age > 20;
select * from students where age between 20 and 40;(这种是包含)
4、查询以姓名以 X 开头的
select * from students where name like ‘x%’;
5、查找字段中为空值得信息
select * from students where classid is null;
6、查找字段值不为空得信息
select * from students where classid is not null;
7、查找报 1,2,6 班得学生信息
select * from students where classid in (1,2,6);
8、查找年龄,并去掉重复得年龄
select distinct age from students;
9、查询年龄,去掉重复并排序
select distinct age from students order by age;(默认正序)
select distinct age from students order by age desc;(倒叙)
10、统计 students 表总共有多少行
select count(*) from students;
11、统计 age 年龄的总和
select sum(age) from students;
12、统计年龄最大的
select max(age) from students;
13、统计男女平均年龄
select gender,avg(age) from students group by gender;
14、分别统计每班的女生男生平均成绩(gender 性别 classid 班级 age 成绩)
select gender,classid,avg(age) from students group by gender, classid;
15、基于上条再统计女生的最大年龄
select gender,max(age) from students group by gender having gender=’f’;
备注:分完组后再条件用 having 不能用 where
16、按照课程统计没课考试最好的成绩
select courseid,max(score) as 最好成绩 from scores group by  courseid;
17、取排序的前 3 名
select age from students order by age desc limit 3;
18、基于排序跳过 2 个显示 3 个
select age from students order by age desc limit 2,3;

多表
1、纵向合并两张表
select stuid as id,name,age,gender from students union select tid,name,age,gender from teachers;
2、基于上条查询 查找 age 字段大于 50 的信息
select * from (select stuid as id,name,age,gender from students union select tid,name,age,gender from teachers)as b  where b.age >50;
备注:利用了子查询

子查询:

01
02 查找比平均年龄大的信息
select name,age from students where age >(select avg(age) from students);

交叉链接

两张表交叉链接组合
select * from students cross join teachers;

内连接

取两张表的交集实现查找出学生对应的老师
select s.name as 学生 name,t.name as 老师 name from students as s inner join teachers as t on s.teacherid=t.tid;
备注:因为两种表有相同的字段,为了群分开给它加别名 as,

左外链接

两张表 A 和 B,
取 A 表和 B 表的与 A 表相关的部分,A 加 B 的一部分
select stuid,s.name,s.age,s.gender,classid,teacherid,tid,t.name,t.age,t.gender from students as s left join teachers as t on s.teacherid=t.tid;

有这样一个表 emp

公司人员信息,即对应的领导 –(leaderid 领导编号)

idnameleaderid
1xiaomingnull
2wanger1
3zhangsan2
4lisi3

现在有这样一个需求,查询每个人员对应的领导是谁
把 emp 表当作两张表来处理,自链接
select e.name as emp,l.name as leader from emp as e left outer join emp as l on e.leaderid=1.id;

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计2402字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中