网站被301跳转,自己如何制作一个软件,网站建设添加资料,如何用excel做网站单表查询 文章目录 单表查询一、单表查询1.1 简单查询1.2where1.3group by1.4having1.5order by1.6limit 一、单表查询
记录的查询语法如下#xff1a; SELECT DISTINCT(去重) 字段1,字段2… FROM 表名 WHERE 筛选条件 GROUP BY 分组 HAVING 分组筛选 ORDER BY 排序 LIMIT 限…单表查询 文章目录 单表查询一、单表查询1.1 简单查询1.2where1.3group by1.4having1.5order by1.6limit 一、单表查询
记录的查询语法如下 SELECT DISTINCT(去重) 字段1,字段2… FROM 表名 WHERE 筛选条件 GROUP BY 分组 HAVING 分组筛选 ORDER BY 排序 LIMIT 限制显示条数
这些关键词的执行顺序是from(选出记录)、where、group by、having、select(根据指定字段保留记录内容)、distinct、order by、limit
1.1 简单查询
#常用的简单查询语句
select * from t1;
select name,age from t1;#对查询的出版社去重
select distinct publish from t1;#带运算和as的查询
#as的作用是将字段重起一个名字
select name,score*100 as Score from t2;#带函数的查询
#concat函数用于连接字符串
select concat(名字:,name) as student_name,concat(分数:,score) as student_score from t3;
#concat_ws函数也用于字符串拼接不过第一个参数为分隔符
select concat_ws(:,name,score) as student from t3;#case的分支语句
select ( case when score85 then 优秀 when score60 then 合格 else 不合格 end) as new_score from t3;
1.2where
where用于对记录进行初步筛选。
#单条件查询
select score from t1 where name张三;#多条件查询select score_Math,score_English from t1 where name张三;#between and表示区间[a,b]
select name from t1 where score between 60 and 100;#not表示取反
select name from t1 where score not between 60 and 100;#is null表示判断字段是否为空需要注意的是不是null
select name form t1 where score is null;
select name form t1 where score is not null;#in表示处于某个范围
#查询成绩为100、90、80分的学生姓名
select name from t1 where score100 or score90 or score80;
select name from t1 where score in [100,90,80];#like表示模糊查询_表示任意字符%表示任何数量的字符
#匹配成绩在80至89的学生姓名
select name from t1 where score like 8_;
#匹配姓张的学生成绩
select score from t1 where name like 张%;#regexp表示正则表达式正则表达式的内容不再介绍了不了解的可以参考下面的链接
#匹配姓张的学生成绩
select score from t1 where name regexp 张.;正则表达式
1.3group by
group by用于对数据进行分组分组的目的是将数据中某一字段的相同内容进行归类并对归类后的数据进行一系列的操作。例如要计算每个部门的平均薪资就需要先对部门进行分组然后对薪资字段使用avg函数聚合。
需要注意的是mysql默认的是ONLY_FULL_GROUP_BY模式的分组简单来说就是分组后查询出来的字段中只能有一个明确的值如果有多个就会报错。例如对部门进行分组查询部门和员工两个字段由于分组后的一个部门含有多个员工这样的分组查询结果会报错。
--------------------------------
| id | name | salary | department |
--------------------------------
| 1 | 王五 | 9000 | IT |
| 2 | 张三 | 10000 | IT |
| 3 | 李四 | 6000 | 销售 |
| 4 | 赵六 | 6500 | 销售 |
--------------------------------#对部门进行分组查询部门和员工两个字段
select department,name from t1 group by department;which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_modeonly_full_group_by#只对部门进行分组
select department from t1 group by department;
------------
| department |
------------
| IT |
| 销售 |
------------#使用group_concat对部门进行分组并查询部门和员工信息
select department,group_concat(name) from t1 group by department;
--------------------------------
| department | group_concat(name) |
--------------------------------
| IT | 王五,张三 |
| 销售 | 李四,赵六 |
--------------------------------#使用聚合函数max、min、avg、count、sum
#求每个部门的平均薪资
select department,avg(salary) from t1 group by department;
-------------------------
| department | avg(salary) |
-------------------------
| IT | 9500.0000 |
| 销售 | 6250.0000 |
-------------------------#求每个部门的员工数
select department,count(name) from t1 group by department;
-------------------------
| department | count(name) |
-------------------------
| IT | 2 |
| 销售 | 2 |
-------------------------#补充聚合函数也可以不跟group by直接使用
#求表中员工的薪资总和
mysql select sum(salary) from t1;
-------------
| sum(salary) |
-------------
| 31500 |
-------------1.4having
having用于对分组结果进行筛选。
--------------------------------
| id | name | salary | department |
--------------------------------
| 1 | 王五 | 9000 | IT |
| 2 | 张三 | 10000 | IT |
| 3 | 李四 | 6000 | 销售 |
| 4 | 赵六 | 6500 | 销售 |
--------------------------------#求出平均薪资大于9000的部门及其薪资
select department,avg(salary) from t1 group by department having avg(salary)9000;
-------------------------
| department | avg(salary) |
-------------------------
| IT | 9500.0000 |
-------------------------1.5order by
order by用于排序其中asc表示升序desc表示降序。(默认为升序)
------------------
| id | num | score |
------------------
| 1 | 10101 | 90 |
| 2 | 10102 | 88 |
| 3 | 10103 | 90 |
------------------#对表中数据按成绩降序排列如果成绩一样则按学号升序排列
select * from t1 order by score desc,num asc;
------------------
| id | num | score |
------------------
| 1 | 10101 | 90 |
| 3 | 10103 | 90 |
| 2 | 10102 | 88 |
------------------
1.6limit
limit用于限制显示的记录数。
------------------
| id | num | score |
------------------
| 1 | 10101 | 90 |
| 2 | 10102 | 88 |
| 3 | 10103 | 90 |
| 4 | 10104 | 70 |
| 5 | 10105 | 100 |
| 6 | 10106 | 79 |
------------------# 查询学号为10101的同学成绩
select * from t1 limit 1;
------------------
| id | num | score |
------------------
| 1 | 10101 | 90 |
------------------# 查询成绩的第二第三名
#limit 1,2表示从第一条记录开始向后显示两条记录(记录从0开始计数)
select * from t1 order by score desc limit 1,2;
------------------
| id | num | score |
------------------
| 1 | 10101 | 90 |
| 3 | 10103 | 90 |
------------------
文章转载自: http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.qzbwmf.cn.gov.cn.qzbwmf.cn http://www.morning.xqnzn.cn.gov.cn.xqnzn.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.ggtgl.cn.gov.cn.ggtgl.cn http://www.morning.spdyl.cn.gov.cn.spdyl.cn http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.wgrl.cn.gov.cn.wgrl.cn http://www.morning.rxfgh.cn.gov.cn.rxfgh.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.kbdrq.cn.gov.cn.kbdrq.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.kqpxb.cn.gov.cn.kqpxb.cn http://www.morning.tzzfy.cn.gov.cn.tzzfy.cn http://www.morning.pggkr.cn.gov.cn.pggkr.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.pfntr.cn.gov.cn.pfntr.cn http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.yrgb.cn.gov.cn.yrgb.cn http://www.morning.wnbqy.cn.gov.cn.wnbqy.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.gmwqd.cn.gov.cn.gmwqd.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.hmbtb.cn.gov.cn.hmbtb.cn http://www.morning.zhqfn.cn.gov.cn.zhqfn.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.gyxwh.cn.gov.cn.gyxwh.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.tbplf.cn.gov.cn.tbplf.cn http://www.morning.crqbt.cn.gov.cn.crqbt.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.qfwzm.cn.gov.cn.qfwzm.cn http://www.morning.lxctl.cn.gov.cn.lxctl.cn http://www.morning.zgpgl.cn.gov.cn.zgpgl.cn http://www.morning.wmfmj.cn.gov.cn.wmfmj.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.tsxg.cn.gov.cn.tsxg.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.fmrd.cn.gov.cn.fmrd.cn http://www.morning.ndcf.cn.gov.cn.ndcf.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.gynls.cn.gov.cn.gynls.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.mytmn.cn.gov.cn.mytmn.cn http://www.morning.ldcsw.cn.gov.cn.ldcsw.cn http://www.morning.ymtbr.cn.gov.cn.ymtbr.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.tturfsoc.com.gov.cn.tturfsoc.com http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.ncwgt.cn.gov.cn.ncwgt.cn http://www.morning.wsnbg.cn.gov.cn.wsnbg.cn http://www.morning.cknsx.cn.gov.cn.cknsx.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.xknsn.cn.gov.cn.xknsn.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.skdrp.cn.gov.cn.skdrp.cn http://www.morning.ssqrd.cn.gov.cn.ssqrd.cn http://www.morning.kqpsj.cn.gov.cn.kqpsj.cn http://www.morning.zcwwb.cn.gov.cn.zcwwb.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn