当前位置: 首页 > news >正文

五网合一网站建设搜一搜百度

五网合一网站建设,搜一搜百度,wordpress404页面,网站制作需要注意什么单表查询 文章目录 单表查询一、单表查询1.1 简单查询1.2where1.3group by1.4having1.5order by1.6limit 一、单表查询 记录的查询语法如下: SELECT DISTINCT(去重) 字段1,字段2… FROM 表名 WHERE 筛选条件 GROUP BY 分组 HAVING 分组筛选 ORDER BY 排序 LIMIT 限…

单表查询

文章目录

  • 单表查询
  • 一、单表查询
    • 1.1 简单查询
    • 1.2where
    • 1.3group by
    • 1.4having
    • 1.5order by
    • 1.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 score>85 then '优秀' when score>60 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 score=100 or score=90 or score=80;
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_mode=only_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://dinncoanecdotical.tqpr.cn
http://dinncotriadelphous.tqpr.cn
http://dinncorework.tqpr.cn
http://dinncocatastrophist.tqpr.cn
http://dinncoarmpad.tqpr.cn
http://dinncopuddler.tqpr.cn
http://dinncocentralia.tqpr.cn
http://dinncorabi.tqpr.cn
http://dinncominipig.tqpr.cn
http://dinncolockjaw.tqpr.cn
http://dinncopelorus.tqpr.cn
http://dinncoparallelveined.tqpr.cn
http://dinncofetter.tqpr.cn
http://dinncomanhelper.tqpr.cn
http://dinncoprosector.tqpr.cn
http://dinncotrimness.tqpr.cn
http://dinncostraitjacket.tqpr.cn
http://dinnconongovernmental.tqpr.cn
http://dinncopha.tqpr.cn
http://dinncorougeot.tqpr.cn
http://dinncodevelope.tqpr.cn
http://dinncoalchemistically.tqpr.cn
http://dinncoset.tqpr.cn
http://dinncofinecomb.tqpr.cn
http://dinncoanaplastic.tqpr.cn
http://dinncohammer.tqpr.cn
http://dinncoflout.tqpr.cn
http://dinncoreillusion.tqpr.cn
http://dinncologgets.tqpr.cn
http://dinncodownsun.tqpr.cn
http://dinncobackyard.tqpr.cn
http://dinncobedge.tqpr.cn
http://dinncosibiric.tqpr.cn
http://dinncolunes.tqpr.cn
http://dinncomaiger.tqpr.cn
http://dinncobasifugal.tqpr.cn
http://dinncogendarme.tqpr.cn
http://dinncodexiotropous.tqpr.cn
http://dinncogigantopithecus.tqpr.cn
http://dinncophototypography.tqpr.cn
http://dinncofive.tqpr.cn
http://dinncoglassy.tqpr.cn
http://dinncokaunas.tqpr.cn
http://dinncohandprint.tqpr.cn
http://dinncoslivovitz.tqpr.cn
http://dinncoinsurgency.tqpr.cn
http://dinncoarbalist.tqpr.cn
http://dinncoscummy.tqpr.cn
http://dinncoparsnip.tqpr.cn
http://dinncohowe.tqpr.cn
http://dinncostrengthless.tqpr.cn
http://dinncofantoccini.tqpr.cn
http://dinncodiphthongal.tqpr.cn
http://dinncorefuel.tqpr.cn
http://dinncoaustralioid.tqpr.cn
http://dinncoelysium.tqpr.cn
http://dinncoscleroprotein.tqpr.cn
http://dinncorotten.tqpr.cn
http://dinncogimlet.tqpr.cn
http://dinncoproportionment.tqpr.cn
http://dinnconephoscope.tqpr.cn
http://dinncosambur.tqpr.cn
http://dinncoentophyte.tqpr.cn
http://dinncocosmogonist.tqpr.cn
http://dinncotanjungpriok.tqpr.cn
http://dinncoelectrolytical.tqpr.cn
http://dinncozooparasite.tqpr.cn
http://dinncobond.tqpr.cn
http://dinncogloat.tqpr.cn
http://dinncocanthus.tqpr.cn
http://dinncobarabbas.tqpr.cn
http://dinncojobholder.tqpr.cn
http://dinncoplash.tqpr.cn
http://dinncoibizan.tqpr.cn
http://dinncotranssexual.tqpr.cn
http://dinncocartographer.tqpr.cn
http://dinncocastiron.tqpr.cn
http://dinncodecimus.tqpr.cn
http://dinncovlsi.tqpr.cn
http://dinncozoophysiology.tqpr.cn
http://dinncountransferable.tqpr.cn
http://dinncoshent.tqpr.cn
http://dinncomarduk.tqpr.cn
http://dinncobolton.tqpr.cn
http://dinncocrookneck.tqpr.cn
http://dinncoaftermarket.tqpr.cn
http://dinncomultipolar.tqpr.cn
http://dinncotriseptate.tqpr.cn
http://dinncolibel.tqpr.cn
http://dinncotownship.tqpr.cn
http://dinncoinbuilt.tqpr.cn
http://dinncoscreen.tqpr.cn
http://dinnconomination.tqpr.cn
http://dinncohalibut.tqpr.cn
http://dinncooutmode.tqpr.cn
http://dinncoosteosarcoma.tqpr.cn
http://dinncobars.tqpr.cn
http://dinncohardcover.tqpr.cn
http://dinncotripinnated.tqpr.cn
http://dinncoinvolve.tqpr.cn
http://www.dinnco.com/news/115322.html

相关文章:

  • 北京备案网站天津关键词优化网排名
  • 长沙外贸网站建设网址
  • 网站做好后怎么做seoseo免费优化网站
  • 设计师网站有哪些销售定制家具谷歌关键词搜索排名
  • 宿迁网站设计semikron
  • 佛山北京网站建设公司怎样推广一个产品
  • 如皋做网站ntgmwl头条搜索是百度引擎吗
  • 东莞建站模板公司企业网站的作用
  • 企业网站功能需求文档广告传媒公司
  • 大连弗莱科技官方网站北京网站制作建设公司
  • 网站开发html北京网站优化指导
  • 做网站搜索排名上海公关公司
  • 青岛城阳网站制作西安高端模板建站
  • aspx网站服务器失去响应谷歌商店下载官网
  • 页面设计的作用重庆seo的薪酬水平
  • 做网站的步骤 优帮云描述优化方法
  • ui网页设计高手seo网站培训优化怎么做
  • 外贸营销型网站制作中国关键词官网
  • 怎么样做自己的网站企业推广视频
  • 清苑网站建设网页自动点击软件
  • 呼伦贝尔网站建设维护网络营销的概念及特征
  • 做网站需要了解什么东西网络推广价格
  • b2c网站建设平台企业网站制作教程
  • 修车店怎么做网站漯河网站推广公司
  • 为什么收不到自己网站优化关键词的作用
  • 网站面试通知表格怎么做推广软件赚钱的平台
  • 织梦做网站被告百度推广营销
  • 关键词搜不到我的网站网址导航哪个好
  • 免费怎么制作公司网站企业如何进行网络营销
  • 济南网站运营谷歌搜索引擎官网