服务端命令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;
备注:利用了子查询

子查询:

查找比平均年龄大的信息
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领导编号)

id name leaderid
1 xiaoming null
2 wanger 1
3 zhangsan 2
4 lisi 3

现在有这样一个需求,查询每个人员对应的领导是谁

把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;

Linux-MySQL基本命令-SQL语句的更多相关文章

  1. Mysql 常用 SQL 语句集锦

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  2. Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  3. MySQL数据库sql语句的一些简单优化

    1.查询条件的先后顺序 有多个查询条件时,要把效率高能更精确筛选记录的条件放在后边.因为MySQL解析sql语句是从后往前的(不知是否准确). 例: select a.*,b.* from UsrIn ...

  4. mysql下sql语句 update 字段=字段+字符串

    mysql下sql语句 update 字段=字段+字符串   mysql下sql语句令某字段值等于原值加上一个字符串 update 表明 SET 字段= 'feifei' || 字段; (postgr ...

  5. MySQL数据库SQL语句基本操作

    一.用户管理: 创建用户: create user '用户名'@'IP地址' identified by '密码'; 删除用户: drop user '用户名'@'IP地址'; 修改用户: renam ...

  6. mysql执行sql语句过程

    开发人员基本都知道,我们的数据存在数据库中(目前最多的是mysql和oracle,由于作者更擅长mysql,所以这里默认数据库为mysql),服务器通过sql语句将查询数据的请求传入到mysql数据库 ...

  7. MySQL与SQL语句的操作

    MySQL与SQL语句的操作 Mysql比较轻量化,企业用的是Oracle,基本的是熟悉对数据库,数据表,字段,记录的更新与修改 1. mysql基本信息 特殊数据库:information_sche ...

  8. mysql 操作sql语句 目录

    mysql 操作sql语句 操作数据库 mysql 操作sql语句 操作数据表 mysql 操作sql语句 操作数据表中的内容/记录

  9. 不登录到MySQL执行SQL语句

    mysql -e 不登录到MySQL执行SQL语句 mysql -u root -p -e "SHOW DATABASES"

  10. mysql优化sql语句

    mysql优化sql语句   常见误区   www.2cto.com   误区1:   count(1)和count(primary_key) 优于 count(*)   很多人为了统计记录条数,就使 ...

随机推荐

  1. echarts散点图重叠

    今天做echarts图标,使用了散点图.很快实现,发现数据不对,应该是3个的企业,页面只显示了2个,查了半天才发现原来是有两个重叠了.想办法解决了,在网上费劲九牛二虎只力终于找到了解决的方法,下面来解 ...

  2. (转)linux du命令

    转自 http://www.cnblogs.com/peida/archive/2012/12/10/2810755.html Linux du命令也是查看使用空间的,但是与df命令不同的是Linux ...

  3. SVN有任何胜过git的地方吗?

    SVN有任何胜过git的地方吗? 好的技术问题通常会引出技术专家们依据经验得出的深层次的观点.但对于这样的问题的答案也很容易演变成完全基于个人喜好的情绪倾泄,而不是根据事实.标准和具体的专业知识.就比 ...

  4. VLAN-3-VLAN Trunk:ISL和802.1Q

      (1)ISL和802.1Q概念       通过使用VLAN Trunk链路,设备可以通过一条链路发送去往多个vlan的流量.为了知道数据帧属于哪个vlan,发送方会添加原始以太网数据帧的头部,这 ...

  5. 采集-telegraf

    1 介绍 Telegraf是一款Go语言编写的metrics收集.处理.聚合的代理其设计目标是较小的内存使用,通过插件来构建各种服务和第三方组件的metrics收集Telegraf由4个独立的插件驱动 ...

  6. 洛谷2758(字符串dp)

    题目传送 记得这是我初学dp时的一道题 虽说就像LCS一样搞一搞即可 但我还是写挂了qwq #include <cstdio> #include <cstring> #incl ...

  7. Codeforces 1139E(二分图最大匹配)

    pi只有0-5000且只找最小的没出现的,又要找不同club的,考虑二分匹配,左边pi,右边ci,一个匹配一个.离线倒着加边即可. const int maxn = 5e3 + 5; int m, n ...

  8. chrome浏览器历史版本

    持续更新中······ google-chrome 浏览器 win64位 版本号 大小 官网更新日期 66.0.3359.181 48.58 MB 2018年5月16日 65.0.3325.181 4 ...

  9. C51 笔记

    一 关于宏常量的长度:C51中定义一个常数宏(默认是16位的),如果用宏表示一个32位的宏而不加'L'标志的话就会出错.如 #define BLOCK_A_BASEADDR  18*64*1024 / ...

  10. HTML5+CSS3新增内容总结!!!!!绝对干货

    说到H5C3会不会觉得东西好多啊,今天就整理了一份总结性的内容: CSS3选择器有哪些?答:属性选择器.伪类选择器.伪元素选择器.CSS3新特性有哪些?答:1.颜色:新增RGBA,HSLA模式 文字阴 ...