MySQL 数据库查询实战操作

本文深入探讨了MySQL数据库的查询操作,从基础的SELECT语句到复杂的联接查询、子查询以及聚合函数的使用,详细阐述了如何高效地获取所需数据。同时,还介绍了优化查询性能的策略,如索引的创建与使用,帮助读者提升数据库查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 、基本查询语句
-- 查询所有姓名为张开头的学生
select * from stuinfo where name like ' %';
-- 查询年龄在 25 岁以上的学生
select * from stuinfo where age>25;
-- 查询家住北京和上海的学生
select * from stuinfo where addr=' 上海 ' or addr=' 北京 ';
select * from stuinfo where addr in(' 上海 ',' 北京 ');
-- 没有留下邮箱的人
select * from stuinfo where email is null;
update stuinfo set email=null where sid=5;
-- 有留下邮箱的人
select * from stuinfo where email is not null;
-- 查询张三的信息
select * from stuinfo where name=' 张三 ';
-- 查询 sid 1 号的学生信息
select * from stuinfo where sid=1;
-- 查询已婚学生信息
select * from stuinfo where mr=1;
-- 查询邮箱为 def 的学生信息
select * from stuinfo where email like '%def%';
-- 查询邮箱首字母为 z l 的学生信息
select * from stuinfo where email like 'z%' or email like 'l%';
-- 查询成绩,按照降序排列 默认是升序
select * from score order by cj desc;
-- 考第一名的学生成绩 limit 0,1 从位置 0 开始,取一个
select * from score order by cj desc limit 0,1;
-- 聚合函数
-- 总共多少学生
select count(*) as 学生总数 from stuinfo;
-- 求总分
select sum(cj) as 总分 from score;
-- 求平均分
select avg(cj) as 平均分 from score;
-- 求最高分
select max(cj) as 最高分 from score;
-- 求最低分 select min(cj) as 最低分 from score;
-- 男生多少人,女生多少人
select sex as 性别 ,count(*) as 个数 from stuinfo group by sex
-- 男生人数
select sex as 性别 ,count(*) as 个数
from stuinfo
group by sex
having sex='m';
select sex as 性别 ,count(*) as 个数
from stuinfo
where sex='m';
-- 每个学生的平均成绩
select sid,avg(cj) from score
group by sid;
-- 求每门课程的平均成绩
select cid,avg(cj) from score
group by cid;
-- 求学号为 2 的学生的平均成绩
select sid,avg(cj) from score
group by sid
having sid=2;
-- 每门课程的最高分是多少分
select cid,max(cj) from score
group by cid
-- 学生分组显示方式 GROUP_CONCAT()
select sid,group_concat(cid),group_concat(cj order by cj desc SEPARATOR ' ') from score
group by sid
-- 每个人平均分,从大到小排
select sid,avg(cj) from score
group by sid
order by avg(cj) desc
2 、函数
-- 时间函数
-- 时间函数 now() sysdate()
select now();
select sysdate();
-- curtime 当前时间
select curtime(),curtime(2) -- curdate 当前日期
select curdate(),curdate()+2;
-- 时间变量
select CURRENT_TIME;
-- 时间函数
SELECT now(),date(now()); -- 日期
SELECT now(),time(now());
SELECT now(),year(now()); --
SELECT now(),quarter(now()); -- 季度
SELECT now(),month(now()); --
SELECT now(),week(now()); --
SELECT now(),day(now()); --
SELECT now(),hour(now()); -- 小时
SELECT now(),minute(now()); -- 分钟
SELECT now(),second(now()); --
SELECT now(),microsecond(now()); -- 微秒
-- 找出出生在 1987 年的学生信息
select * from stuinfo
select * from stuinfo where year(birth)='1987';
-- 找出所有的 90
select * from stuinfo where year(birth) BETWEEN 1990 and 1999
-- 字符串函数 见表

 

-- length utf-8 汉字占有 3 个字节
select length('sql 课程 ') -- 返回 9
select CHAR_LENGTH('sql 课程 ') -- 返回 5
-- right left 左右截图
select right(' 买买提 , 土尔松 ',3)
select left(' 买买提 , 土尔松 ',3)
-- concat 字符串连接
select CONCAT(' 买买提 ',',',' 土尔松 ')
-- 将数据库中所有姓李的改成姓王的
update stuinfo set name=concat(' ',RIGHT(name,CHAR_LENGTH(name)-1)) where name like '
%';
-- ORD 函数 显示 ascii
select ORD('2');
select ORD('abc');
-- mid 函数
-- mid() 函数用于得到一个字符串的一部分。这个函数被 MySQL 支持,但不被 MS SQL Server Oracle 支持。在 SQL Server Oracle 数据库中,我们可以使用 SQL SUBSTRING 函数或者
SQL SUBSTR 函数作为替代。从指定位置取规定个数子串,下标 1 开始
select mid('abcd',2,2)
-- sleep 函数 延时
select sleep(5);
3 union 查询
union 查询就是把 2 条或者多条 sql 语句的查询结果,合并成一个结果集。
如: sql1: N 行, sql2: M 行, sql1 union sql2 ---> N+M
1 、能否从 2 张表查询再 union ?
可以 ,union 合并的是 " 结果集 ", 不区分在自于哪一张表 .
2 、取自于 2 张表 , 通过 " 别名 " 2 个结果集的列一致。那么 , 如果取出的结果集 , 列名字不一
, 还能否 union.
可以 , 而且取出的最终列名 , 以第 1 sql 为准
3 union 满足什么条件就可以用了 ?
只要结果集中的列数一致就可以 . (如都是 2 列或者 N 列)
4 union 后结果集 , 可否再排序呢 ?
可以的。 Sql1 union sql2 order by 字段
注意 : order by 是针对合并后的结果集排的序 .
5 、如果 Union 后的结果有重复 ( 即某 2 , N , 所有的列 , 值都一样 ), 怎么办 ?
这种情况是比较常见的 , 默认会去重 .
6 、如果不想去重怎么办 ?
union all
-- unoin 查询去重 union all 不去重
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值