简单查询
SELECT语句
SELECT [DISTINCT] *或者{column1,column2...} FROM table_name;
--DISTINCT表示去除重复项,*表示查询所有记录或者指定查询的列名
--例一:查询学生表中的数据(学生表:id,name,math,chinese,english)
select * from student;
--例二:查询表中所有学生姓名和对应的英语成绩
select name,english from student;
--例三:过滤表中重复数据
select distinct * from student;
--例四:在所有学生分数上加10分特长分
select name,math+10,english+10,chinese+10 from student;
--例五:统计每个学生的总分
select name,math+english+chinese from student;
--例六:使用别名表示总成绩
select name as 姓名,math+english+chinese as 总成绩 from student;--注:as可省略
带有WHERE字句的过滤查询
比较运算符 | <, >, <=, >= ,=,<> | 大于,小于,小于等于,大于等于,等于,不等于 |
between...and... | 显示在某一区间内的值 | |
in(set) | 显示在in列表中的值,如:in(100,200) | |
like '张pattern' | 模糊查询%_ | |
is null | 判断是否为空 | |
逻辑运算符 | and | 多个条件同时成立 |
or | 多个条件任一成立 | |
not | 不成立,如:where not(salary>100); | |
注意:lke中,%代表零个或多个任意字符,_代表一个字符,例如:first_name like '_a%'; |
--例一:查询姓名为zs的学生成绩
SELECT * from student where name = 'zs';
--例二:查询英语成绩大于90的学生
select * from student where english>90;
--例三:查询总分大于230分的所有同学
select * from student where math+english+chinese>230;
--例四:查询英语在80到100之间的同学
select * from student where english between 80 and 100;
--例五:查询数学分数为75,76,77的同学
select * from student where math in(75,76,77);
--例六:查询所有姓张的学生
select * from student where name like '张%';
--例七:查询数学分数>70,语文分数>80的同学
select * from student where math>70 and chinese>80;
高级查询
聚合函数
<1>count聚合函数,用来统计行数
SELECT couint(*)或者count(列名) FROM table_name [WHERE where_definition];
--例一:统计一个班有多少学生
select count(*) from student;
--例二:统计数学成绩大于90的人数
select count(*) from student where math>80;
--例三:统计总分大于230的人数
select count(*) from student where math+english+chinese>230;
<2>sum 聚合函数,求符合条件的某列的和值
SELECT sum(列名) [,sum(列名)...] FROM table_name [WHERE where_definition];
--例一:统计一个班数学总成绩
select sum(math) from student;
--例二:统计一个班语文数学英语各科总成绩
select sum(math),sum(english),sum(chinese) from student;
--例三:统计一个班语文数学英语的成绩总和
select sum(math+english+chinese) from student;
--例四:统计一个班语文成绩平均分
select sum(chinese)/count(*) from student;
<3>avg聚合函数,求符合条件的列的平均值
SELECT avg(列名)[,avg(列名)...] FROM table_name [WHERE where_definition];
--例一:求一个班的数学平均分
select avg(math) from student;
--例二:求一个班级总分平均分
select avg(math+english+chinese) from student;
<4>max和min聚合函数,求符合条件列的最大值和最小值
SELECT max(列名) FROM table_name [WHERE where_definition];
--例一:求班级最高分和最低分
select max(math+english+chinese) from student;select min(math+english+chinese) from student;
排序查询(order by)
SELECT column1,column2... FROM table_name order by column asc或者desc;
--例一:对英语成绩排序并输出
elect name,english from student order by english;--默认升序,若需要降序则使用desc
--例二:对总分降序排列输出
select name 姓名, math+english+chinese 总分 from student order by 总分 desc;
--例三:对名字以张开头的学生总成绩排序
select name 姓名, math+english+chinese 总分 from student where name like '张%' order by 总分;注意:order by在语句结尾。
分组查询(group by)
SELECT column1,column2,... FROM table_name GROUP BY column HAVING ...
--使用HAVING字句对分组结果进行过滤,
--WHERE和HAVING的区别:where在分组前进行过滤,having在分组后进行过滤。where可以用having代替,但是where中不能使用分组函数
--例一:对订单表(表orders)中商品归类后,显示每一类商品的总价
select product,sum(price) from orders group by product;--分组后可使聚合函数,计算每组price的总和
--例二:查询总价大于100元的商品名称
select product,sum(price) from orders group by product having sum(price)>100;
--例三:查询单价小于100而总价大于100的商品名称
select product,sum(price) from orders where price<100 group by product having sum(price)>100;
使用LIMIT限制查询结果的数量
SELECT 字段名1,字段名2,... FROM 表名 LIMIT [OFFSET,] 记录数;
--例一:查询学生表前五个记录
select * from student limit 0,5;
MySQL中的其他常用函数
<1>数学函数
函数名称 | 作用 |
---|---|
ABS(x) | 返回x 的绝对值 |
SQRT(x) | 返回x的非负2次方根 |
MOD(x,y) | 返回x被y除后的余数 |
CEILING(x) | 返回不小于x的最小 整数 |
FLOOR(x) | 返回不大于x的最大整数 |
ROUND(x,y) | 对x进行四舍五入操作,小数点后保留y位 |
TRUNCATE(x,y) | 舍取x中小数点y位后面的数 |
SIGN(x) | 返回x的符号,-1,0,1 |
<2>字符串函数
函数名称 | 作用 |
---|---|
LENGTH(str) | 返回字符串str的长度 |
CONCAT(s1,s2,…) | 返回一个或者多个字符串连接产生的新的字符串 |
TRIM(str) | 删除字符串两侧的空格 |
REPLACE(str,s1,s2) | 使用字符串s2替换字符串str中所有的字符串s1 |
SUBSTRING(str,n,len) | 返回字符串str的子串,起始位置为n,长度为len |
REVERSE(str) | 返回字符串反转后的效果 |
LOCATE(s1,str) | 返回字符串s1在字符串str中的起始位置 |
<3>日期和时间函数
函数名称 | 作用 |
---|---|
CURDATE() | 获取系统当前日期 |
CURTIME() | 获取系统当前日期 |
SYSDATE() | 获取当前系统日期和时间 |
TIME_TO_SEC() | 返回将时间转换成秒的结果 |
ADDDATE() | 执行日期的加运算 |
SBUDATE() | 执行日期的减运算 |
DATE_FORMAT() | 格式化输出日期和时间 |
<4>条件判断函数
函数名称 | 作用 |
---|---|
IF(expr,v1,v2) | 如果expr表达式为true返回v1,否则返回v2 |
IFNULL(v1,v2) | 如果v1不为NULL返回v1,否则返回v2 |
CASE expr WHEN v1 THEN r1 [WHEN v2 THEN r2…] [ELSE m] END | 如果expr的值等于v1,v2 |
<5>加密函数
函数名称 | 作用 |
---|---|
MD5(str) | 对字符串进行MD5加密 |
ENCODE(str,pwd_str) | 使用pwd作为密码加密字符串 |
DECODE(str,pwd_str) | 使用pwd作为密码解密字符串 |
为表和字段取别名
为表名取别名
SELECT * FROM 表名 [AS] 别名;
例如:select * from student as s where s.grade > 80;
为字段取别名
SELECT 字段名 [AS] 别名 [,字段名 [AS]...] FROM 表名;