MySQL数据库之select单表查询

select与各种语句搭配查询

  1. select语句详解
    • 2 where语句
      • 3 like模糊查询语句
        • 4 group by 语句
          • 5 having 语句
            • 6 order by 语句
              • 7 limit 语句

1,select语句

功能:获取表中的数据行
语法:select 输出列表达式;
1.1 select单独使用
(1)select配合内置函数使用
== 案例:==
1查询当前时间

查询当前时间
mysql> select now();
±--------------------+
| now() |
±--------------------+
| 2020-06-11 16:57:05 |
±--------------------+
1 row in set (0.00 sec)

2,查询用户用户名和白名单

mysql> select concat(user,"&",host) from mysql.user;
±---------------------------+
| concat(user,"&",host) |
±---------------------------+
| mysql.infoschema&localhost |
| mysql.session&localhost |
| mysql.sys&localhost |
| root&localhost |
±---------------------------+
4 rows in set (0.01 sec)

3,计算

mysql> select 100/10;
±--------------------+
| 100/10 |
±--------------------+
| 10.0000 |
±--------------------+
1 row in set (0.00 sec)

2,select标准用法

–单表
select

  1. from 表1 ,表2,…
  2. where 过滤条件1 过滤条件2 …
  3. group by 条件列1 条件列2
  4. having 过滤条件1 过滤条件2 …
  5. order by 条件列1 条件列2
  6. limit 限制

注:如果这些语句都有在写的时候一定要严格按照这个顺序来

2.1 select 配合 from 子句的使用
语法: select 列 from 表 ;
案例:
1,查询表中的所有数据
1.1查询学生表(student)的所有数据

mysql>select * from student;
或者
mysql> select id,name from student ;

2,查询部分列的功能
2.1查询name列

mysql> select name from student ;
如果数据太多想要分页显示我们可以使用limit 例如十行一页
mysql> select name from student limit 10;

2.2 where 语句 ==》 相当于grep功能

语法:SELECT field1, field2,…fieldN FROM table_name1, >table_name2…
[WHERE condition1 [AND [OR]] condition2…
2.2.1where 配合比较判断符号 = ,> ,< ,<= ,>= ,!=

案例1:查询city中,中国所有城市信息

mysql>select * from world.city where countrycode=‘CH’ ;

案例2:查询city表中 ,人口数量小于1000人的城市

mysql>select * from world.city where population<1000 ;

2.3where 配合 like 语句 模糊查询

语法:SELECT field1, field2,…fieldN
FROM table_name
WHERE field1 LIKE condition1 [AND [OR]] filed2 = ‘somevalue’

案例1:查询city中,国家代号是CH打头的城市信息

mysql>select * from world.city where countrycode likke ‘CH%’;

注意:like语句在使用时,切记不要出现前面带%的模糊查询,因为不走索引
错误案例:

mysql>select * from world.city where countrycode like ‘%CH%’;

切记只能字符串类的列才能用like,其他的int什么的不能,除非把int类型的写成字符串

2.4where配合逻辑连接符and or
案例1:查询中国人口大于500W的城市

mysql>#and 即求并集条件的公共部分
mysql>select * from world.city where countrycode=‘CH’ and population>5000000;

案例2:查询中国或美国的城市信息

mysql>select * from world.city where countrycode=‘CH’ or countrycode=‘USA’ ;

2.5 where 配合 between and 使用
案例1:查询城市人口数在100W到200W之间的

mysql>#方法一
mysql>select * from world.city where population>=1000000 and population<=2000000;
mysql>#方法二
mysql>select * from world.city where population between 1000000 and 2000000 ;

2.5 select + from + where + group by 配合使用
group by 配合各种聚合函数使用

语法:SELECT column_name, function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

聚合函数:
max() :最大值
min() :最小值
avg() :平均值
count() :统计个数
sum() :求和
group_concat :列转行

说明:碰到group by 必然会有聚合函数
案例1:统计city中,每个国家的城市个数

mysql>#思路分析:我们要统计的是成绩个数,所以我们可以用国家来分组,再利用count聚合函数来实现

mysql>select countrycode,count(id)   from world.city  group by countrycode ; 

案例2:统计中国,每个省的城市个数

mysql>#照样先进行思路分析 首先我们先要找到中国的省份,再按照省份进行分组

mysql>select district,count(id)  from world.city where countrycode='CHN' group by  district ;

案例3:统计中国,每个省总人口,城市个数,城市名列表

mysql>select district ,sum(population),count(id),group_concat(name)
   -->from world.city
   -->where countrycode='CHN'
   -->group by district ;

2.6 having 语句

语法:SELECT column_name, function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
having column_name operator value;

作用:进行后过滤
案例1:统计中国,每个省的总人口,只显示总人口数大于500w的

mysql>select district ,sum(population),count(id),group_concat(name)
   -->from world.city
   -->where countrycode='CHN'
   -->group by district
   -->having sum(population) > 5000000 ;

2.7 order by 语句

语法:SELECT field1, field2,…fieldN FROM table_name1, table_name2…
ORDER BY field1 [ASC [DESC][默认 ASC]], [field2…] [ASC [DESC][默认 ASC]]

案例1:统计中国,每个省的总人口,只显示总人口数大于500w信息,并且按照从大到小的顺序输出

mysql>select district ,sum(population),count(id),group_concat(name)
   -->from world.city
   -->where countrycode='CHN'
   -->group by district
   -->having sum(population) > 5000000
   -->order by sum(population) desc ;

2.8 limit 语句

语法:SELECT field1, field2,…fieldN FROM table_name1, table_name2…
limit 偏移量 offset 偏移量
前面一个偏移量为总共显示的行数,第二个偏移量为跳过的行数

作用:分页显示结果集
案例1:统计中国,每个省的总人口,只显示总人口数大于500w信息,并且按照从大到小的顺序输出,只显示前五名

mysql>select district ,sum(population),count(id),group_concat(name)
   -->from world.city
   -->where countrycode='CHN'
   -->group by district
   -->having sum(population) > 5000000
   -->order by sum(population) desc
   -->limit 5 ;

案例二:统计中国,每个省的总人口,只显示总人口数大于500w信息,并且按照从大到小的顺序输出,显示3~5名

mysql>select district ,sum(population),count(id),group_concat(name)
   -->from world.city
   -->where countrycode='CHN'
   -->group by district
   -->having sum(population) > 5000000
   -->order by sum(population) desc
   -->limit 3 offset 2 ;

好了单表查询就介绍到这里了,多表查询下期更新,大家对此有什么更简便的方法欢迎评论区分享。

在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值