一、查询结果去重
distinct
select distinct university from user_profile;
二、查询结果限制返回行数
limit n
select device_id from user_profile limit 2;
三、查询后的列取别名
as
select device_id as user_infos_example from user_profile
四、查询后排序
group by
select device_id,age from user_profile order by age;
多列排序
select device_id,gpa,age from user_profile order by gpa,age;
降序排序desc
select
device_id,gpa,age
from user_profile
order by gpa desc,age desc;
五、利用运算符构建查询条件
1、等于=
select device_id,university from user_profile where university = '北京大学';
2、大于>,小于<,大于等于>=,小于等于<=
select device_id,gender,age,university from user_profile where age > 24;
3、两数范围内between and
select
device_id,gender,age
from
user_profile
where age between 20 and 23;
4、不等于!=,<>
-- <>
select
device_id,gender,age,university
from
user_profile
where
university <> '复旦大学';
-- !=
select
device_id,gender,age,university
from
user_profile
where
university != '复旦大学';
5、非空is not null,为空is null
select
device_id,gender,age,university
from
user_profile
where
age is not null;
6、逻辑运算符
(1)与and
select
device_id,gender,age,university,gpa
from
user_profile
where
gpa > 3.5 and gender = 'male';
(2)或or
select
device_id,gender,age,university,gpa
from
user_profile
where
gpa > 3.5 and gender = 'male';
7、模糊查询like
select
device_id,gender,age,university,gpa
from
user_profile
where
university like '%北%大%';
在单引号中的内容中%表示一串不定长度的内容,_表示一个未知的字符。若要表示这两个字符时需要使用转义字符\,即%和_。
8、范围查询in (‘’[,…n])
该操作表示连续的=和or的合用。
select
device_id,gender,age,university,gpa
from
user_profile
where
university in ('北京大学','复旦大学','山东大学');
代码中的university in (‘北京大学’,‘复旦大学’,‘山东大学’);等价于university = ‘北京大学’ or university = ‘复旦大学’ or university = ‘山东大学’。
六、聚合查询
1、最大值max(),最小值min()
select
max(gpa) as gpa
from
user_profile
where
university = '复旦大学';
2、总数count(参数列表),平均值avg(列名字)
select
count(*) as male_num,avg(gpa) as avg_gpa
from
user_profile
group by gender
having gender='male';
3、聚合查询的除法运算
select
u.university as university,
count(q.result)/count(distinct q.device_id) as avg_answer_cnt
from
user_profile as u,
question_practice_detail as q
where
u.device_id = q.device_id
group by
university
order by
university asc;
七、组合查询
将两个查询结果列名相同的查询语句连在一起,查询结果将排在一起。
union查询结果去重
union all查寻结果不去重
select
device_id,gender,age,gpa
from
user_profile
where
university = '山东大学'
union all
select
device_id,gender,age,gpa
from
user_profile
where
gender = 'male';
八、分支构建新的查询列
1、case when
(1)语法格式
(case
when 逻辑条件 then 展示结果
...
else 最终结果
end)
(2)代码案例
select
(case
when age<25 then '25岁以下'
when age>=25 then '25岁及以上'
else '25岁以下'
end) as age_cut,
count(*) as number
from
user_profile
group by age_cut;
2、if
(1)语法格式
if(逻辑条件,成立执行内容,不成立执行内容)
if的多重条件只能嵌套。
九、日期函数
1、日期获取函数
(1)当前时间获取函数
-- 获取当前日期(年月日)
curdate();
-- 获取当前时间(时分秒)
curtime();
-- 获取当下时间(年月日 时分秒)
now()
(2)指定部分获取函数
-- 获取指定时间的年月日
DATE('2022-09-10 13:56:10');
-- 获取指定时间的时分秒
TIME('2022-09-10 13:56:10');
-- 获取指定时间的年
year('2022-09-10 13:56:10');
-- 获取指定时间的月
month('2022-09-10 13:56:10');
-- 获取指定时间的日
day('2022-09-10 13:56:10');
-- 获取指定时间的季度
quarter('2022-09-10 13:56:10');
-- 获取指定时间的第几周
weekofyear('2022-09-10 13:56:10');
-- 获取指定时间的时
hour('2022-09-10 13:56:10');
-- 获取指定时间的分
minute('2022-09-10 13:56:10');
-- 获取指定时间的秒
second('2022-09-10 13:56:10');
-- 指定日期部分提取函数
extract(year from '2022-09-10 13:56:10');
extract(quarter from '2022-09-10 13:56:10');
2、日期计算函数
-- 日期加固定时长
DATE_ADD('2022-9-10',INTERVAL 3 YEAR)-- 2025-9-10
-- 日期减固定时长
DATE_SUB('2022-9-10',INTERVAL 3 YEAR)-- 2019-9-10
-- 计算差多少天
DATEDIFF('2022-9-10','2022-9-11')-- -1