mysql_select 单表查询

select *         *代表全部
 
查询多个字段
select  字段1,字段2,字段3
 
聚合函数  count(*)       统计 select count(*)
                sum() 求和 select sum() 或 select sum(cj)
                avg() 平均值 select avg() 或 select avg(cj)
                max() 最大值 select max() 或 select max(cj)
                min() 最小值 select min() 或 select min(cj),min(cj)
示例:
#查询001号学生的最高分、最低分、总分、平均分以及考了几科
select max(cj),min(cj),sum(cj),avg(cj),count(xh)
from cjb
where xh='001'
 
distinct  
去重
整单查找单独去重一个结果  count(distinct 条件)
#查询1024班的学生来自哪些个省市 去重(distinct)
select distinct jg as '省市'
from xsb
where bj='1024'       
 
as   字段别名
form     从….表名
 
where +条件
1、精确查询:例:指定查询什么。。where xm='张三'
2、模糊查询:like ...像 %:匹配任意个数字符
                       _:匹配一个字符
select *
from xsb
where xm like '%冰%'

3、逻辑运算(多个条件时,适用) and 且 or 或

#查询张三或李四,且年龄大于20的详细信息
select *
from xsb
where (xm='张三'or xm='李四') and nl>'20'

4、条件运算符    >,<,>=,<=

#查询年龄在20至23之间的学生信息(包含20和23)
select *
from xsb
where nl<=23 and nl>=20

5、集合运算符            in 在        not in 不在

#查询来自河北、河南、山东以外省市的学生信息
select *
from xsb
where jg not in ('河北','河南','山东')

6、非空运算

is null 是空
is not null 是不为空
#查询身份证号为空的学生信息
select *
from xsb
where sfzh is null
 
#查询身份证号不为空的学生信息
select *
from xsb
where sfzh is not null

7、区间运算

between...and…
表示一个闭区间
#查询年龄在20至23之间的学生信息(包含20和23)
select *
from xsb
where nl between 20 and 23
 

group by +条件       分组字段

按该字段的值进行分组,值相同的为一组
字段1,字段2
分组嵌套,先按字段1进行分组,
然后对分完后的每个组再按字段2进行二次分组
#统计学生表中的男女生人数
select count(*),xb
from xsb
group by xb

 

分组后的条件
having 
分组后的条件
如应用必须出现在group by后面
having后面一般跟聚合函数
#查询平均分大于75的学生学号
select avg(cj),xh
from cjb
group by xh
having avg(cj)>75


order by查询结果 升序和降序
order by 条件 desc   降序
order by 条件     升序 asc默认不写
如应用必须出现在group by后面
#3) 查找高等教育出版社的 所有图书及单价,结果按单价降序排序。
select sm,dj
from book
where cbdw='高等教育出版社' 
order by dj desc
 
limit (放在最后) 
从第n+1条记录开始取m条记录     n、m
#查询成绩表的全部信息,按成绩降序排列,(取前3名)
select * select * 
from cjb from cjb
order by cj desc order by cj desc
limit 0,3 limit 3
 
 
 

主外键连接

references   关联
forgien key (外键) references 主表(主键)
 
truncate  去小数后位数
truncate(max(dj),2)
去除小数后2位
原文地址:https://www.cnblogs.com/lixy-88428977/p/9564345.html