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
不去重