当前位置:网站首页>Tutorial on the principle and application of database system (059) -- MySQL exercise questions: operation questions 1-10 (III)
Tutorial on the principle and application of database system (059) -- MySQL exercise questions: operation questions 1-10 (III)
2022-07-28 13:45:00 【Rsda DBA_ WGX】
Database system principle and Application Tutorial (059)—— MySQL Exercises : Operation questions 1-10( 3、 ... and )
1、 Basic query (1): Query all columns in the table
subject : To view all the information in the user information table , To write SQL Statement to get the corresponding result .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | province |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | Beijing |
| 2 | 3214 | male | Fudan University | Shanghai | |
| 3 | 6543 | female | 20 | Peking University, | Beijing |
| 4 | 2315 | female | 23 | Zhejiang University | ZheJiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
The query should return the following results :
| id | device_id | gender | age | university | province |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | Beijing |
| 2 | 3214 | male | Fudan University | Shanghai | |
| 3 | 6543 | female | 20 | Peking University, | Beijing |
| 4 | 2315 | female | 23 | Zhejiang University | Zhejiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
Table structure and data are as follows :
/* 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,' Peking University, ','BeiJing'); INSERT INTO user_profile VALUES(2,3214,'male',null,' Fudan University ','Shanghai'); INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ','BeiJing'); INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ','ZheJiang'); INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ','Shandong'); */
answer :
-- Use *
mysql> select * from user_profile;
+----+-----------+--------+------+--------------+----------+
| id | device_id | gender | age | university | province |
+----+-----------+--------+------+--------------+----------+
| 1 | 2138 | male | 21 | Peking University, | BeiJing |
| 2 | 3214 | male | NULL | Fudan University | Shanghai |
| 3 | 6543 | female | 20 | Peking University, | BeiJing |
| 4 | 2315 | female | 23 | Zhejiang University | ZheJiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
+----+-----------+--------+------+--------------+----------+
5 rows in set (0.00 sec)
-- Use the field list
mysql> select id, device_id, gender, age, university, province from user_profile;
+----+-----------+--------+------+--------------+----------+
| id | device_id | gender | age | university | province |
+----+-----------+--------+------+--------------+----------+
| 1 | 2138 | male | 21 | Peking University, | BeiJing |
| 2 | 3214 | male | NULL | Fudan University | Shanghai |
| 3 | 6543 | female | 20 | Peking University, | BeiJing |
| 4 | 2315 | female | 23 | Zhejiang University | ZheJiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
+----+-----------+--------+------+--------------+----------+
5 rows in set (0.00 sec)
2、 Basic query (2): Query multiple columns
subject : To query the user's device id The corresponding gender 、 Age and school information , Please take out the corresponding data , To write SQL Statement to get the corresponding result .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | province |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | Beijing |
| 2 | 3214 | male | Fudan University | Shanghai | |
| 3 | 6543 | female | 20 | Peking University, | Beijing |
| 4 | 2315 | female | 23 | Zhejiang University | Zhejiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
The query should return the following results :
| device_id | gender | age | university |
|---|---|---|---|
| 2138 | male | 21 | Peking University, |
| 3214 | male | Fudan University | |
| 6543 | female | 20 | Peking University, |
| 2315 | female | 23 | Zhejiang University |
| 5432 | male | 25 | Shandong University |
answer :
mysql> select device_id, gender, age, university from user_profile;
+-----------+--------+------+--------------+
| device_id | gender | age | university |
+-----------+--------+------+--------------+
| 2138 | male | 21 | Peking University, |
| 3214 | male | NULL | Fudan University |
| 6543 | female | 20 | Peking University, |
| 2315 | female | 23 | Zhejiang University |
| 5432 | male | 25 | Shandong University |
+-----------+--------+------+--------------+
5 rows in set (0.00 sec)
3、 Basic query (3): Query result de duplication
subject : To see which schools users come from , Get the school's de duplication data from the user information table .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | province |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | Beijing |
| 2 | 3214 | male | Fudan University | Shanghai | |
| 3 | 6543 | female | 20 | Peking University, | Beijing |
| 4 | 2315 | female | 23 | Zhejiang University | ZheJiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
The query should return the following results :
| university |
|---|
| Peking University, |
| Fudan University |
| Zhejiang University |
| Shandong University |
answer :
mysql> select distinct university from user_profile;
+--------------+
| university |
+--------------+
| Peking University, |
| Fudan University |
| Zhejiang University |
| Shandong University |
+--------------+
4 rows in set (0.00 sec)
4、 Basic query (4): Limit the number of rows returned from query results
subject : Before you want to view 2 Devices of users ID data , From the user information table user_profile Take out the corresponding results .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | province |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | Beijing |
| 2 | 3214 | male | Fudan University | Shanghai | |
| 3 | 6543 | female | 20 | Peking University, | Beijing |
| 4 | 2315 | female | 23 | Zhejiang University | ZheJiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
The query should return the following results :
| device_id |
|---|
| 2138 |
| 3214 |
answer :
mysql> select device_id from user_profile limit 2;
+-----------+
| device_id |
+-----------+
| 2138 |
| 3214 |
+-----------+
2 rows in set (0.00 sec)
5、 Basic query (5): Specify an alias for the queried column
subject : Before you want to view 2 Devices of users ID, And change the column name to ‘user_infos_example’, Take the corresponding results from the user information table .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | province |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | Beijing |
| 2 | 3214 | male | Fudan University | Shanghai | |
| 3 | 6543 | female | 20 | Peking University, | Beijing |
| 4 | 2315 | female | 23 | Zhejiang University | ZheJiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
The query should return the following results :
| user_infos_example |
|---|
| 2138 |
| 3214 |
answer :
-- Don't use as
mysql> select device_id user_infos_example from user_profile limit 2;
+--------------------+
| user_infos_example |
+--------------------+
| 2138 |
| 3214 |
+--------------------+
2 rows in set (0.01 sec)
-- Use as
mysql> select device_id as user_infos_example from user_profile limit 2;
+--------------------+
| user_infos_example |
+--------------------+
| 2138 |
| 3214 |
+--------------------+
2 rows in set (0.00 sec)
6、 Comparison operator (1): The inquiry school is 【 Peking University, 】 Student information
subject : To screen out all the student information of Peking University , Take the data that meets the conditions from the user information table , Return device id And school .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | province |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | Beijing |
| 2 | 3214 | male | Fudan University | Shanghai | |
| 3 | 6543 | female | 20 | Peking University, | Beijing |
| 4 | 2315 | female | 23 | Zhejiang University | ZheJiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
The query should return the following results :
| device_id | university |
|---|---|
| 2138 | Peking University, |
| 6543 | Peking University, |
answer :
mysql> select device_id, university from user_profile where university = ' Peking University, ';
+-----------+--------------+
| device_id | university |
+-----------+--------------+
| 2138 | Peking University, |
| 6543 | Peking University, |
+-----------+--------------+
2 rows in set (0.00 sec)
7、 Comparison operator (2): Query age is greater than 24 Years old user information
subject : To query 24 User information older than , Take out the equipment that meets the conditions ID、 Gender 、 Age 、 School .
User information sheet :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | province |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | Beijing |
| 2 | 3214 | male | Fudan University | Shanghai | |
| 3 | 6543 | female | 20 | Peking University, | Beijing |
| 4 | 2315 | female | 23 | Zhejiang University | ZheJiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
The query should return the following results :
| device_id | gender | age | university |
|---|---|---|---|
| 5432 | male | 25 | Shandong University |
answer :
mysql> select device_id, gender, age, university from user_profile where age > 24;
+-----------+--------+------+--------------+
| device_id | gender | age | university |
+-----------+--------+------+--------------+
| 5432 | male | 25 | Shandong University |
+-----------+--------+------+--------------+
1 row in set (0.01 sec)
8、 Comparison operator (3): Find user information for a certain age group
subject : To query 20 Years old and older and 23 User information aged and under , Take out the qualified equipment ID、 Gender 、 Age .
User information sheet :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | province |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | Beijing |
| 2 | 3214 | male | Fudan University | Shanghai | |
| 3 | 6543 | female | 20 | Peking University, | Beijing |
| 4 | 2315 | female | 23 | Zhejiang University | ZheJiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
The query should return the following results :
| device_id | gender | age |
|---|---|---|
| 2138 | male | 21 |
| 6543 | female | 20 |
| 2315 | female | 23 |
answer :
-- Use between ... and ...
mysql> select device_id, gender, age from user_profile where age between 20 and 23;
+-----------+--------+------+
| device_id | gender | age |
+-----------+--------+------+
| 2138 | male | 21 |
| 6543 | female | 20 |
| 2315 | female | 23 |
+-----------+--------+------+
3 rows in set (0.01 sec)
-- Use comparison operators and logical operators
mysql> select device_id, gender, age from user_profile where age >= 20 and age <= 23;
+-----------+--------+------+
| device_id | gender | age |
+-----------+--------+------+
| 2138 | male | 21 |
| 6543 | female | 20 |
| 2315 | female | 23 |
+-----------+--------+------+
3 rows in set (0.00 sec)
9、 Comparison operator (4): Find user information except Fudan University
subject : To view all user information except Fudan University , Take out the device id, Gender , Age , School .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | province |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | Beijing |
| 2 | 3214 | male | Fudan University | Shanghai | |
| 3 | 6543 | female | 20 | Peking University, | Beijing |
| 4 | 2315 | female | 23 | Zhejiang University | ZheJiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
The query should return the following results :
| device_id | gender | age | university |
|---|---|---|---|
| 2138 | male | 21 | Peking University, |
| 6543 | female | 20 | Peking University, |
| 2315 | female | 23 | Zhejiang University |
| 5432 | male | 25 | Shandong University |
answer :
-- Use <>
mysql> select device_id, gender, age, university from user_profile where university <> ' Fudan University ';
+-----------+--------+------+--------------+
| device_id | gender | age | university |
+-----------+--------+------+--------------+
| 2138 | male | 21 | Peking University, |
| 6543 | female | 20 | Peking University, |
| 2315 | female | 23 | Zhejiang University |
| 5432 | male | 25 | Shandong University |
+-----------+--------+------+--------------+
4 rows in set (0.00 sec)
-- Use not
mysql> select device_id, gender, age, university from user_profile where not university = ' Fudan University ';
+-----------+--------+------+--------------+
| device_id | gender | age | university |
+-----------+--------+------+--------------+
| 2138 | male | 21 | Peking University, |
| 6543 | female | 20 | Peking University, |
| 2315 | female | 23 | Zhejiang University |
| 5432 | male | 25 | Shandong University |
+-----------+--------+------+--------------+
4 rows in set (0.00 sec)
10、 Comparison operator (5): Filter for null values
subject : To view users whose age is not empty , Take out the user's device ID, Gender , Age , School information .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | province |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | Beijing |
| 2 | 3214 | male | Fudan University | Shanghai | |
| 3 | 6543 | female | 20 | Peking University, | Beijing |
| 4 | 2315 | female | 23 | Zhejiang University | ZheJiang |
| 5 | 5432 | male | 25 | Shandong University | Shandong |
The query should return the following results :
| device_id | gender | age | university |
|---|---|---|---|
| 2138 | male | 21 | Peking University, |
| 6543 | female | 20 | Peking University, |
| 2315 | female | 23 | Zhejiang University |
| 5432 | male | 25 | Shandong University |
answer :
-- Use age is not null
mysql> select device_id, gender, age, university from user_profile where age is not null;
+-----------+--------+------+--------------+
| device_id | gender | age | university |
+-----------+--------+------+--------------+
| 2138 | male | 21 | Peking University, |
| 6543 | female | 20 | Peking University, |
| 2315 | female | 23 | Zhejiang University |
| 5432 | male | 25 | Shandong University |
+-----------+--------+------+--------------+
4 rows in set (0.00 sec)
-- Use not age is null
mysql> select device_id, gender, age, university from user_profile where not age is null;
+-----------+--------+------+--------------+
| device_id | gender | age | university |
+-----------+--------+------+--------------+
| 2138 | male | 21 | Peking University, |
| 6543 | female | 20 | Peking University, |
| 2315 | female | 23 | Zhejiang University |
| 5432 | male | 25 | Shandong University |
+-----------+--------+------+--------------+
4 rows in set (0.00 sec)
边栏推荐
- Strict mode -- let and const -- arrow function -- Deconstruction assignment -- string template symbol -- set and map -- generator function
- Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development
- 剖析 kubernetes 集群内部 DNS 解析原理
- Kotlin learning notes 3 - lambda programming
- 今日睡眠质量记录75分
- SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误的试读版
- SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response
- Night God simulator packet capturing wechat applet
- P1797重型运输 题解
- Humiliation, resistance, reversal, 30 years, China should win Microsoft once
猜你喜欢

Auto.js enables Taobao to quickly submit orders

30天刷题计划(四)

Denial of service DDoS Attacks

Better and more modern terminal tools than xshell!

111. The sap ui5 fileuploader control realizes local file upload and encounters a cross domain access error when receiving the response from the server

Map tiles: detailed explanation of vector tiles and grid tiles

用非递归的方法实现二叉树中的层遍历,先序遍历,中序遍历和后序遍历
JWT login authentication + token automatic renewal scheme, well written!

Half wave rectification light LED

Have you seen the management area decoupling architecture? Can help customers solve big problems
随机推荐
《暗黑破坏神4》PS4/PS5测试版已加入PlayStation数据库
PHP generates random numbers (nickname random generator)
After finishing, help autumn move, I wish you call it an offer harvester
powerdesigner创建数据库模型(概念模型举例)
I'm bald! Who should I choose for unique index or general index?
蓝桥集训(附加面试题)第七天
今日睡眠质量记录75分
Today's sleep quality record 75 points
Dry goods -- encapsulated anti shake and throttling method in the project
验证码暴力破解测试[通俗易懂]
Using auto.js to realize fifaol3 brush teaching assistant
国产API管理工具Eolink太好用了,打造高效的研发利器
DDoS protection with iptables
Have a part of the game, after NFT is disabled in my world
屈辱、抗争、逆转,三十年,中国该赢微软一次了
Intra prediction and transform kernel selection based on Neural Network
org.apache.ibatis.exceptions.TooManyResultsException的异常排查过程
Kotlin learning notes 3 - lambda programming
Facial expression recognition based on pytorch convolution - graduation project "suggestions collection"
【安全】 阅读 RFC6749 及理解 Oauth2.0 下的授权码模式