Skip to the content.

01-入门基础


常用术语



执行顺序

  1. FROM
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. DISTINCT
  7. UNION
  8. ORDER BY

注意:



检索数据

select语句:选择列

-- 检索所有的列
select *
from tablename;

-- 检索多个列
select id, name
from tablename;

-- 检索不同的值
select distinct id
from tablename;

limit语句:限制行数

-- 限制前100行数据
select *
from tablename
limit 100;

-- 返回第5行(不含)之后的100行数据
select *
from tablename
limit 100 offset 5;

-- 返回第5行(不含)之后的100行数据(简写)
select *
from tablename
limit 5,100;



排序数据

order by语句:排序

-- 默认升序
select apply_date, name
from tablename
order by apply_date;

-- 多列排序:apply_date升序,name降序
select apply_date, name
from tablename
order by apply_date, name desc;

-- 按位置排序:第1列升序,第2列降序
select apply_date, name
from tablename
order by 1, 2 desc;



过滤数据

where语句:增加过滤条件

select *
from tablename
where apply_date = '2021-01-21';

where语句常用操作符

select *
from tablename
where apply_date <> '2021-01-21';

select *
from tablename
where apply_date >= '2021-01-21';
-- 文本值范围
select *
from tablename
where grade in ('A','B');

-- 数值范围(包含范围两端)
select *
from tablename
where age between 2 and 10;
select *
from tablename
where name is (not) null;
-- 开头模糊
select *
from tablename
where name like '%ing';

-- 末尾模糊
select *
from tablename
where name like 'Fish%';

-- 两端模糊
select *
from tablename
where name like '%is%';
select *
from tablename
where grade in ('A','B')
and apply_date >= '2021-01-21';



创建计算字段

拼接字段

select concat(id, '-', name) as new_col
from tablename;

算术计算

select period * amount as new_col
from tablename;

条件计算

select 
 apply_date
,if(state=1,'approve','reject') as state1
,case state
 when 1 then 'approve'
 when 2 then 'reject'
 else 'other'
 end as state2
,case when state = 1 then 'approve'
 	  when state = 2 then 'reject'
 	  else 'other' end as state3
from tablename;



汇总数据

聚合函数

select
 cont(distinct *) `总行数`
,count(id) `总人数`
,sum(amount) `总金额`
,avg(amount) `平均金额`
from tablename;

group by语句:数据分组

select 
 date_format(apply_time,'%Y-%m') `月份`
,count(id) `总人数`
from tablename
group by 1
order by 1;

-- 分组并展示细分汇总
select 
 province
,city
,count(id) `总人数`
from tablename
group by 1,2 with rollup

having语句:过滤分组

select 
 date_format(apply_time,'%Y-%m') `月份`
,count(id) `总人数`
from tablename
group by 1
having count(id) >= 30
order by 1;



子查询

子查询作为过滤数据条件

select *
from tablename1
where id in
            (
            select id
            from tablename2
            where grade = 'A'
            );

子查询作为汇总数据前提

select id, avg(amount)
from 
    (
    select id, amount
    from tablename
    where grade = 'A'
    )
group by 1;

子查询作为计算字段

select
 id
,(
  select count(*)
  from tablename2
  where tablename2.id = tablename1.id
 ) as cnt
from tablename1;


运用with as创建临时表进行子查询

with 
temp1 as
(
select id, amount
from tablename1
where grade = 'A'
),

temp2 as
(
select id, name
from tablename2
)

select *
from temp1
left join temp2 on temp1.id = temp2.id;



联结表

union语句:纵向关联

-- union:自动去除重复行
select id, amount
from tablename1
union
select id, amount
from tablename1

-- union all:保留所有重复行
select id, amount
from tablename1
union all
select id, amount
from tablename1

join语句:横向关联

select 
 t1.id
,t1.name
,t2.grade
,t2.score
from tablename1 t1
left join tablename2 t2 on t1.id = t2.id;

内联结的两种写法

-- 方法1:inner join
select 
 t1.id
,t1.name
,t2.grade
,t2.score
from tablename1 t1
inner join tablename2 t2 on t1.id = t2.id;

-- 方法2:where
select 
 t1.id
,t1.name
,t2.grade
,t2.score
from tablename1 t1, tablename2 t2
where t1.id = t2.id;

横向无条件联结

select 
 t1.id
,t2.name
from tablename1 t1
left join tablename2 t2
on 1 = 1;