0
点赞
收藏
分享

微信扫一扫

MySQL单表数据查询

舍予兄 2022-06-01 阅读 63

单表查询

drop table if exists person;
create table person(
    id int primary key,
    name varchar(20)
);

insert into person values (1, 'BNTang');
insert into person values (2, 'JonathanTang');

语法

  • select * from 表名;
  • 查询表中所有数据

select * from person;

  • select 字段1, 字段2 from 表名;
  • 查询表中指定字段数据

select `name` from person;

  • select [* || 字段] from 表名 [where 条件];
  • 查询表中满足条件的数据

select `name` from person where id = 1;

结果集

  • 通过查询语句查询出来的结果我们就称之为​​结果集​
  • 结果集以表的形式将查询的结果返回给我们

????注意点

  • 结果集返回的表和查询的表不是同一张表
  • 被查询的表是真实存在的, 是存储在磁盘上的
  • 而结果集不是真实存在的, 是存储到内存中的

给结果集的字段起别名

  • 查询指定字段数据时, 我们可以通过​​as​​给指定字段取别名

drop table if exists stu;
create table stu(
    id int auto_increment primary key,
    name varchar(20),
    gender enum('','','')
);

insert into stu values (null, '张三', '');
insert into stu values (null, '李四', '');

SELECT name as MyName, gender as MyGender FROM stu;

字段表达式

  • 查询数据的时候, 除了可以查询指定字段的数据以外, 我们还可以查询表达式的结果

SELECT 6+6;

伪表

  • 字段表达式虽然能够查询出表达式的结果, 但是不符合MySQL的规范
  • 所以我们可以通过伪表(dual)的方式让字段表达式符合MySQL的规范

SELECT 6+6 from dual;

模糊查询

格式:select 字段 from 表名 where 字段 like '条件';

  • ​_​​通配符:表示任意一个字符
  • ​%​​通配符:表示任意0~n个字符

a_c:abc、adc、abc、adc、abbc、ac
_a_c:1abc、3adc、1abc、abc1、2abbc、3adc
a%c:abc、adc、abbc、ac、abc、adc、abbc、ac
%a%c:1abc、2abbc、3adc、1abc、abc1、2abbc、3adc

select * from stu where name like '张_';

select * from stu where name like '张__';

select * from stu where name like '张_%';

排序

通过关键字​​order by​​进行排序

语法:select 字段 from 表名 order by 字段 [asc | desc];

select * from stu order by id;# 默认按照升序进行排序

select * from stu order by id asc;# 升序排序

select * from stu order by id desc;# 降序排序

select * from stu order by id desc, gender asc;# 如果年龄相同, 那么还可以继续按照其它字段来排序

聚合函数

????count():统计

select count(*) from stu;

select count(*) from stu where id >= 1;

????sum():求和

select sum(id) from stu;

????avg():求平均值

select avg(id) from stu;

????max():获取最大值

select max(id) from stu;

????min():获取最小值

select min(id) from stu;

数值类

????rand():生成随机数

select rand() from dual;

select * from stu order by rand();

????round():四舍五入

select round(3.1) from dual;

select round(3.5) from dual;

????ceil():向上取整

select ceil(3.1) from dual;

????floor():向下取整

select floor(3.9) from dual;

????truncate():截取小数位

select truncate(3.1234567, 2) from dual;

字符串类

????ucase():转换为大写

select ucase('hello world') from dual;

????lcase():转换为小写

select lcase('HELLO WORLD') from dual;

????left():从左边开始截取到指定的位置

select left('1234567890', 3) from dual;

????right():从右边开始截取到指定的位置

select right('1234567890', 3) from dual;

????substring():从指定位置开始截取指定个字符

select substring('1234567890', 3, 5) from dual;

数据分组

使用关键字​​group by​​进行分组

语法:select 分组字段 || 聚合函数 from 表名 group by 分组字段;

  • 需求:要求统计表中一共有多少个城市

DROP TABLE IF EXISTS `stu`;
CREATE TABLE `stu`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gender` enum('','','') CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `city` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

INSERT INTO `stu` VALUES (1, '张三', '', '上海');
INSERT INTO `stu` VALUES (2, '李四', '', '北京');

select city from stu;

select city from stu group by city;

  • 需求:要求统计每个城市中有多少个人

select city, count(*) from stu group by city;

????注意点

  • 在对数据进行分组的时候, select 后面必须是分组字段或者聚合函数, 否则就只会返回第一条数据

select city from stu group by city;

select name from stu group by city;

select city, group_concat(name) from stu group by city;

条件查询

使用关键字​​having​​进行条件查询

  • having和where很像都是用来做条件查询的
  • 但是where是去数据库中查询符合条件的数据, 而having是去结果集中查询符合条件的数据

select * from stu where city='北京';

select * from stu having city='北京';

select `name`,gender from stu where city='北京';

select `name`, gender from stu having city='北京';# Unknown column 'city' in 'having clause'

  • 需求:select city from stu group by city;
  • 需求:select city, avg(gender) from stu group by city;
  • 需求:select city, avg(gender) as average from stu group by city;
  • 需求:select city, avg(gender) as average from stu group by city having average>=2;

分页

使用关键字​​limit​​进行分页

语法:select 字段 from 表 limit 索引, 个数;

select * from stu limit 0, 3;

select * from stu limit 3, 3;

查询选项

语法:select [查询选项] 字段名称 from 表名;

  • all:显示所有查询出来的数据[默认]
  • distinct:去除结果集中重复的数据之后再显示

select `name` from stu;

select all `name` from stu;

select distinct `name` from stu;

????注意点

  • 如果是通过​​distinct​​来对结果集中重复的数据进行去重
  • 那么只有所有列的数据都相同才会去重

select `name`, `city` from stu;

select distinct `name`, `city` from stu;

完整的查询语句

select [查询选项] 字段名称 [from 表名] [where 条件] [order by 排序] [group by 分组] [having 条件] [limit 分页];

举报

相关推荐

0 条评论