【第1天】SQL快速入门-基础查询(SQL 小虚竹)

x33g5p2x  于2022-08-17 转载在 其他  
字(3.3k)|赞(0)|评价(0)|浏览(351)

回城传送–》《32天SQL筑基》

零、前言

今天是学习 SQL 打卡的第1天,每天我会提供一篇文章供群成员阅读( 不需要订阅付钱 )。

希望大家先自己思考,如果实在没有想法,再看下面的解题思路,自己再实现一遍。在小虚竹JAVA社区 中对应的 【打卡贴】打卡,今天的任务就算完成了,养成每天学习打卡的好习惯。

​ 虚竹哥会组织大家一起学习同一篇文章,所以有什么问题都可以在群里问,群里的小伙伴可以迅速地帮到你,一个人可以走得很快,一群人可以走得很远,有一起学习交流的战友,是多么幸运的事情。

​ 我的学习策略很简单,题海策略+ 费曼学习法。如果能把这些题都认认真真自己实现一遍,那意味着 SQL 已经筑基成功了。后面的进阶学习,可以继续跟着我,一起走向架构师之路。

今天的学习内容是:基础查询

一、练习题目

题目链接难度
SQL2 查询多列★☆☆☆☆
SQL3 查询结果去重★☆☆☆☆
SQL4 查询结果限制返回行数★☆☆☆☆
SQL5 将查询后的列重新命名★☆☆☆☆

二、SQL思路

1、SQL2 查询多列

初始化数据

drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`province` varchar(32)  NOT NULL);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

解法

1、运营同学想要用户的设备id对应的性别、年龄和学校的数据,只需要列这几个字段就行,通常我们在写查询语句时,千万不要写
select * from table_name

这种是很耗时,耗性能的。

select
  device_id,
  gender,
  age,
  university
from
  user_profile

2、SQL3 查询结果去重

初始化数据

drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`province` varchar(32)  NOT NULL);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

解法

1、这题有两种解法,一个是使用去重关键词:distinct

select distinct university from user_profile;

2、第二种解法是使用分组,对某个字段分组,也是有去重效果。
关键词: group by

select university from user_profile group by university;

3、SQL4 查询结果限制返回行数

初始化数据

drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`province` varchar(32)  NOT NULL);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

解法

1、第一种解法:要取前2条数据,如果只是如表中的数据,可以取id<=2的数据

select device_id from user_profile where id<=2

2、第二种解法:使用mysql的 limit 关键字。只给定一个参数,它表示返回最大的记录行数目。( limit m 检索前 m 个记录行)

select device_id from user_profile limit 2

3、第三种解法:使用mysql的 limit 关键字。第一个参数指定第一个返回记录行的偏移量(偏移量是 0 ),第二个参数指定返回记录行的最大数目。

select device_id from user_profile limit 0,2

4、SQL5 将查询后的列重新命名

初始化数据

drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`province` varchar(32)  NOT NULL);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

解法

第一种解法:使用关键词:AS

select device_id as user_infos_example from user_profile limit 0,2

第二种解法:

select 旧列名 新列名 from table_name

select device_id  user_infos_example from user_profile limit 0,2

相关文章