当前位置:网站首页>Tutorial on the principle and application of database system (061) -- MySQL exercise: operation questions 21-31 (V)
Tutorial on the principle and application of database system (061) -- MySQL exercise: operation questions 21-31 (V)
2022-07-28 13:45:00 【Rsda DBA_ WGX】
Database system principle and Application Tutorial (061)—— MySQL Exercises : Operation questions 21-31( 5、 ... and )
21、 Link query (1)
subject : To view the answers of all users from Zhejiang University , Take out the corresponding data .
Example :question_practice_detail The data in the table are as follows .
| id | device_id | question_id | result |
|---|---|---|---|
| 1 | 2138 | 111 | wrong |
| 2 | 3214 | 112 | wrong |
| 3 | 3214 | 113 | wrong |
| 4 | 6543 | 114 | right |
| 5 | 2315 | 115 | right |
| 6 | 2315 | 116 | right |
| 7 | 2315 | 117 | wrong |
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | Fudan University | 4.0 | 15 | 5 | 25 | |
| 3 | 6543 | female | 20 | Peking University, | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | Shandong University | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | female | 26 | Fudan University | 3.6 | 9 | 6 | 52 |
The query should return the following results , The query results are based on question_id Ascending sort :
| device_id | question_id | result |
|---|---|---|
| 2315 | 115 | right |
| 2315 | 116 | right |
| 2315 | 117 | wrong |
Table structure and data are as follows :
/* drop table if exists `user_profile`; drop table if exists `question_practice_detail`; 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, `gpa` float, `active_days_within_30` int , `question_cnt` int , `answer_cnt` int ); CREATE TABLE `question_practice_detail` ( `id` int NOT NULL, `device_id` int NOT NULL, `question_id`int NOT NULL, `result` varchar(32) NOT NULL ); INSERT INTO user_profile VALUES(1,2138,'male',21,' Peking University, ',3.4,7,2,12); INSERT INTO user_profile VALUES(2,3214,'male',null,' Fudan University ',4.0,15,5,25); INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ',3.2,12,3,30); INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ',3.6,5,1,2); INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ',3.8,20,15,70); INSERT INTO user_profile VALUES(6,2131,'male',28,' Shandong University ',3.3,15,7,13); INSERT INTO user_profile VALUES(7,4321,'male',28,' Fudan University ',3.6,9,6,52); INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong'); INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong'); INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong'); INSERT INTO question_practice_detail VALUES(4,6543,111,'right'); INSERT INTO question_practice_detail VALUES(5,2315,115,'right'); INSERT INTO question_practice_detail VALUES(6,2315,116,'right'); INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong'); */
answer :
/* -- Use connection query select q.device_id, q.question_id, q.result from question_practice_detail q join user_profile u on q.device_id = u.device_id where u.university = ' Zhejiang University '; -- Use subquery select device_id, question_id, result from question_practice_detail where device_id in (select device_id from user_profile where university = ' Zhejiang University '); */
-- Use connection query
mysql> select q.device_id, q.question_id, q.result
-> from question_practice_detail q join user_profile u
-> on q.device_id = u.device_id
-> where u.university = ' Zhejiang University ';
+-----------+-------------+--------+
| device_id | question_id | result |
+-----------+-------------+--------+
| 2315 | 115 | right |
| 2315 | 116 | right |
| 2315 | 117 | wrong |
+-----------+-------------+--------+
3 rows in set (0.00 sec)
-- Use subquery
mysql> select device_id, question_id, result
-> from question_practice_detail
-> where device_id in
-> (select device_id from user_profile where university = ' Zhejiang University ');
+-----------+-------------+--------+
| device_id | question_id | result |
+-----------+-------------+--------+
| 2315 | 115 | right |
| 2315 | 116 | right |
| 2315 | 117 | wrong |
+-----------+-------------+--------+
3 rows in set (0.01 sec)
22、 Link query (2)
subject : Query the average number of users who have answered questions in each school , Take out relevant data .
Example :user_profile The data in the table are as follows .
| device_id | gender | age | university | gpa | active_days_within_30 |
|---|---|---|---|---|---|
| 2138 | male | 21 | Peking University, | 3.4 | 7 |
| 3214 | male | NULL | Fudan University | 4 | 15 |
| 6543 | female | 20 | Peking University, | 3.2 | 12 |
| 2315 | female | 23 | Zhejiang University | 3.6 | 5 |
| 5432 | male | 25 | Shandong University | 3.8 | 20 |
| 2131 | male | 28 | Shandong University | 3.3 | 15 |
| 4321 | male | 28 | Fudan University | 3.6 | 9 |
Example :question_practice_detail The data in the table are as follows .
| device_id | question_id | result |
|---|---|---|
| 2138 | 111 | wrong |
| 3214 | 112 | wrong |
| 3214 | 113 | wrong |
| 6543 | 111 | right |
| 2315 | 115 | right |
| 2315 | 116 | right |
| 2315 | 117 | wrong |
| 5432 | 118 | wrong |
| 5432 | 112 | wrong |
| 2131 | 114 | right |
| 5432 | 113 | wrong |
Find the average number of answers per school user ( explain : The average number of questions answered by users in a school is calculated by dividing the total number of answers by the number of different users who have answered the questions ), The query should return the following results ( The result is reserved 4 Decimal place ), according to university Ascending sort .
| university | avg_answer_cnt |
|---|---|
| Peking University, | 1.0000 |
| Fudan University | 2.0000 |
| Shandong University | 2.0000 |
| Zhejiang University | 3.0000 |
Table structure and data are as follows :
/* drop table if exists `user_profile`; drop table if exists `question_practice_detail`; CREATE TABLE `user_profile` ( `device_id` int NOT NULL, `gender` varchar(14) NOT NULL, `age` int , `university` varchar(32) NOT NULL, `gpa` float, `active_days_within_30` int ); CREATE TABLE `question_practice_detail` ( `device_id` int NOT NULL, `question_id`int NOT NULL, `result` varchar(32) NOT NULL ); INSERT INTO user_profile VALUES(2138,'male',21,' Peking University, ',3.4,7); INSERT INTO user_profile VALUES(3214,'male',null,' Fudan University ',4.0,15); INSERT INTO user_profile VALUES(6543,'female',20,' Peking University, ',3.2,12); INSERT INTO user_profile VALUES(2315,'female',23,' Zhejiang University ',3.6,5); INSERT INTO user_profile VALUES(5432,'male',25,' Shandong University ',3.8,20); INSERT INTO user_profile VALUES(2131,'male',28,' Shandong University ',3.3,15); INSERT INTO user_profile VALUES(4321,'male',28,' Fudan University ',3.6,9); INSERT INTO question_practice_detail VALUES(2138,111,'wrong'); INSERT INTO question_practice_detail VALUES(3214,112,'wrong'); INSERT INTO question_practice_detail VALUES(3214,113,'wrong'); INSERT INTO question_practice_detail VALUES(6543,111,'right'); INSERT INTO question_practice_detail VALUES(2315,115,'right'); INSERT INTO question_practice_detail VALUES(2315,116,'right'); INSERT INTO question_practice_detail VALUES(2315,117,'wrong'); INSERT INTO question_practice_detail VALUES(5432,118,'wrong'); INSERT INTO question_practice_detail VALUES(5432,112,'wrong'); INSERT INTO question_practice_detail VALUES(2131,114,'right'); INSERT INTO question_practice_detail VALUES(5432,113,'wrong'); */
answer :
/* select u.university, round(count(*)/count(distinct u.device_id),4) avg_answer_cnt from question_practice_detail q join user_profile u on q.device_id = u.device_id group by u.university order by u.university; */
mysql> select u.university,
-> round(count(*)/count(distinct u.device_id),4) avg_answer_cnt
-> from question_practice_detail q join user_profile u
-> on q.device_id = u.device_id
-> group by u.university
-> order by u.university;
+--------------+----------------+
| university | avg_answer_cnt |
+--------------+----------------+
| Peking University, | 1.0000 |
| Fudan University | 2.0000 |
| Shandong University | 2.0000 |
| Zhejiang University | 3.0000 |
+--------------+----------------+
4 rows in set (0.00 sec)
23、 Link query (3)
subject : Check the different schools that participated in the answer 、 The average number of questions answered by users with different difficulties , Take out the corresponding data .
Example :user_profile( User information sheet ) The data are as follows .
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | NULL | Fudan University | 4 | 15 | 5 | 25 |
| 3 | 6543 | female | 20 | Peking University, | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | Shandong University | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | male | 28 | Fudan University | 3.6 | 9 | 6 | 52 |
Example :question_practice_detail( List of exercises in question bank ) The data are as follows .
| id | device_id | question_id | result |
|---|---|---|---|
| 1 | 2138 | 111 | wrong |
| 2 | 3214 | 112 | wrong |
| 3 | 3214 | 113 | wrong |
| 4 | 6534 | 111 | right |
| 5 | 2315 | 115 | right |
| 6 | 2315 | 116 | right |
| 7 | 2315 | 117 | wrong |
| 8 | 5432 | 117 | wrong |
| 9 | 5432 | 112 | wrong |
| 10 | 2131 | 113 | right |
| 11 | 5432 | 113 | wrong |
| 12 | 2315 | 115 | right |
| 13 | 2315 | 116 | right |
| 14 | 2315 | 117 | wrong |
| 15 | 5432 | 117 | wrong |
| 16 | 5432 | 112 | wrong |
| 17 | 2131 | 113 | right |
| 18 | 5432 | 113 | wrong |
| 19 | 2315 | 117 | wrong |
| 20 | 5432 | 117 | wrong |
| 21 | 5432 | 112 | wrong |
| 22 | 2131 | 113 | right |
| 23 | 5432 | 113 | wrong |
Example :question_detail( User information sheet ) The data are as follows .
| id | question_id | difficult_level |
|---|---|---|
| 1 | 111 | hard |
| 2 | 112 | medium |
| 3 | 113 | easy |
| 4 | 115 | easy |
| 5 | 116 | medium |
| 6 | 117 | easy |
Please write out SQL sentence , Calculate different schools 、 The average number of questions answered by users with different difficulties , The query should return the following results ( Data rounding is reserved 4 Decimal place ):
| university | difficult_level | avg_answer_cnt |
|---|---|---|
| Peking University, | hard | 1.0000 |
| Fudan University | easy | 1.0000 |
| Fudan University | medium | 1.0000 |
| Shandong University | easy | 4.5000 |
| Shandong University | medium | 3.0000 |
| Zhejiang University | easy | 5.0000 |
| Zhejiang University | medium | 2.0000 |
Table structure and data are as follows :
/* drop table if exists `user_profile`; drop table if exists `question_practice_detail`; drop table if exists `question_detail`; 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, `gpa` float, `active_days_within_30` int , `question_cnt` int , `answer_cnt` int ); CREATE TABLE `question_practice_detail` ( `id` int NOT NULL, `device_id` int NOT NULL, `question_id`int NOT NULL, `result` varchar(32) NOT NULL ); CREATE TABLE `question_detail` ( `id` int NOT NULL, `question_id`int NOT NULL, `difficult_level` varchar(32) NOT NULL ); INSERT INTO user_profile VALUES(1,2138,'male',21,' Peking University, ',3.4,7,2,12); INSERT INTO user_profile VALUES(2,3214,'male',null,' Fudan University ',4.0,15,5,25); INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ',3.2,12,3,30); INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ',3.6,5,1,2); INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ',3.8,20,15,70); INSERT INTO user_profile VALUES(6,2131,'male',28,' Shandong University ',3.3,15,7,13); INSERT INTO user_profile VALUES(7,4321,'male',28,' Fudan University ',3.6,9,6,52); INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong'); INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong'); INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong'); INSERT INTO question_practice_detail VALUES(4,6543,111,'right'); INSERT INTO question_practice_detail VALUES(5,2315,115,'right'); INSERT INTO question_practice_detail VALUES(6,2315,116,'right'); INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong'); INSERT INTO question_practice_detail VALUES(8,5432,117,'wrong'); INSERT INTO question_practice_detail VALUES(9,5432,112,'wrong'); INSERT INTO question_practice_detail VALUES(10,2131,113,'right'); INSERT INTO question_practice_detail VALUES(11,5432,113,'wrong'); INSERT INTO question_practice_detail VALUES(12,2315,115,'right'); INSERT INTO question_practice_detail VALUES(13,2315,116,'right'); INSERT INTO question_practice_detail VALUES(14,2315,117,'wrong'); INSERT INTO question_practice_detail VALUES(15,5432,117,'wrong'); INSERT INTO question_practice_detail VALUES(16,5432,112,'wrong'); INSERT INTO question_practice_detail VALUES(17,2131,113,'right'); INSERT INTO question_practice_detail VALUES(18,5432,113,'wrong'); INSERT INTO question_practice_detail VALUES(19,2315,117,'wrong'); INSERT INTO question_practice_detail VALUES(20,5432,117,'wrong'); INSERT INTO question_practice_detail VALUES(21,5432,112,'wrong'); INSERT INTO question_practice_detail VALUES(22,2131,113,'right'); INSERT INTO question_practice_detail VALUES(23,5432,113,'wrong'); INSERT INTO question_detail VALUES(1,111,'hard'); INSERT INTO question_detail VALUES(2,112,'medium'); INSERT INTO question_detail VALUES(3,113,'easy'); INSERT INTO question_detail VALUES(4,115,'easy'); INSERT INTO question_detail VALUES(5,116,'medium'); INSERT INTO question_detail VALUES(6,117,'easy'); */
answer :
/* select u.university, qd.difficult_level, round(count(*)/count(distinct u.device_id),4) avg_answer_cnt from user_profile u join question_practice_detail q on u.device_id = q.device_id join question_detail qd on q.question_id = qd.question_id group by u.university, qd.difficult_level; */
mysql> select u.university, qd.difficult_level,
-> round(count(*)/count(distinct u.device_id),4) avg_answer_cnt
-> from user_profile u join question_practice_detail q
-> on u.device_id = q.device_id
-> join question_detail qd on q.question_id = qd.question_id
-> group by u.university, qd.difficult_level;
+--------------+-----------------+----------------+
| university | difficult_level | avg_answer_cnt |
+--------------+-----------------+----------------+
| Peking University, | hard | 1.0000 |
| Fudan University | easy | 1.0000 |
| Fudan University | medium | 1.0000 |
| Shandong University | easy | 4.5000 |
| Shandong University | medium | 3.0000 |
| Zhejiang University | easy | 5.0000 |
| Zhejiang University | medium | 2.0000 |
+--------------+-----------------+----------------+
7 rows in set (0.00 sec)
24、 Link query (4)
subject : Query the average number of questions answered by users of Shandong University who participated in the answer under different difficulties , Take out the corresponding data .
Example :user_profile( User information sheet ) The data are as follows .
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | NULL | Fudan University | 4 | 15 | 5 | 25 |
| 3 | 6543 | female | 20 | Peking University, | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | Shandong University | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | male | 28 | Fudan University | 3.6 | 9 | 6 | 52 |
Example :question_practice_detail( List of exercises in question bank ) The data are as follows .
| id | device_id | question_id | result |
|---|---|---|---|
| 1 | 2138 | 111 | wrong |
| 2 | 3214 | 112 | wrong |
| 3 | 3214 | 113 | wrong |
| 4 | 6534 | 111 | right |
| 5 | 2315 | 115 | right |
| 6 | 2315 | 116 | right |
| 7 | 2315 | 117 | wrong |
| 8 | 5432 | 117 | wrong |
| 9 | 5432 | 112 | wrong |
| 10 | 2131 | 113 | right |
| 11 | 5432 | 113 | wrong |
| 12 | 2315 | 115 | right |
| 13 | 2315 | 116 | right |
| 14 | 2315 | 117 | wrong |
| 15 | 5432 | 117 | wrong |
| 16 | 5432 | 112 | wrong |
| 17 | 2131 | 113 | right |
| 18 | 5432 | 113 | wrong |
| 19 | 2315 | 117 | wrong |
| 20 | 5432 | 117 | wrong |
| 21 | 5432 | 112 | wrong |
| 22 | 2131 | 113 | right |
| 23 | 5432 | 113 | wrong |
Example :question_detail( User information sheet ) The data are as follows .
| id | question_id | difficult_level |
|---|---|---|
| 1 | 111 | hard |
| 2 | 112 | medium |
| 3 | 113 | easy |
| 4 | 115 | easy |
| 5 | 116 | medium |
| 6 | 117 | easy |
The query should return the following results ( Data rounding is reserved 4 Decimal place ):
| university | difficult_level | avg_answer_cnt |
|---|---|---|
| Shandong University | easy | 4.5000 |
| Shandong University | medium | 3.0000 |
answer :
/* select u.university, qd.difficult_level, round(count(*)/count(distinct u.device_id),4) avg_answer_cnt from user_profile u join question_practice_detail q on u.device_id = q.device_id join question_detail qd on q.question_id = qd.question_id where u.university = ' Shandong University ' group by u.university, qd.difficult_level; */
mysql> select u.university, qd.difficult_level,
-> round(count(*)/count(distinct u.device_id),4) avg_answer_cnt
-> from user_profile u join question_practice_detail q
-> on u.device_id = q.device_id
-> join question_detail qd on q.question_id = qd.question_id
-> where u.university = ' Shandong University '
-> group by u.university, qd.difficult_level;
+--------------+-----------------+----------------+
| university | difficult_level | avg_answer_cnt |
+--------------+-----------------+----------------+
| Shandong University | easy | 4.5000 |
| Shandong University | medium | 3.0000 |
+--------------+-----------------+----------------+
2 rows in set (0.00 sec)
25、 The joint query (union)
subject : View users whose school is Shandong University or whose gender is male device_id、gender、age and gpa data , Take out the corresponding results ( The result is no weight loss ).
Example :user_profile The data are as follows .
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | Fudan University | 4 | 15 | 5 | 25 | |
| 3 | 6543 | female | 20 | Peking University, | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | Shandong University | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | male | 26 | Fudan University | 3.6 | 9 | 6 | 52 |
The query should return the following results :
| device_id | gender | age | gpa |
|---|---|---|---|
| 5432 | male | 25 | 3.8 |
| 2131 | male | 28 | 3.3 |
| 2138 | male | 21 | 3.4 |
| 3214 | male | None | 4 |
| 5432 | male | 25 | 3.8 |
| 2131 | male | 28 | 3.3 |
| 4321 | male | 28 | 3.6 |
answer :
/* select device_id, gender, age, gpa from user_profile where university= ' Shandong University ' union all select device_id, gender, age, gpa from user_profile where gender = 'male'; */
mysql> select device_id, gender, age, gpa from user_profile where university= ' Shandong University '
-> union all
-> select device_id, gender, age, gpa from user_profile where gender = 'male';
+-----------+--------+------+------+
| device_id | gender | age | gpa |
+-----------+--------+------+------+
| 5432 | male | 25 | 3.8 |
| 2131 | male | 28 | 3.3 |
| 2138 | male | 21 | 3.4 |
| 3214 | male | NULL | 4 |
| 5432 | male | 25 | 3.8 |
| 2131 | male | 28 | 3.3 |
| 4321 | male | 28 | 3.6 |
+-----------+--------+------+------+
7 rows in set (0.00 sec)
26、IF Use of functions
subject : Divide users into 25 Under and 25 Two age groups aged and over , Query the number of users in these two age groups (age by null It's also recorded as 25 Under the age of ).
Example :user_profile The data are as follows .
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | Fudan University | 4 | 15 | 5 | 25 | |
| 3 | 6543 | female | 20 | Peking University, | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | Shandong University | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | male | 26 | Fudan University | 3.6 | 9 | 6 | 52 |
The query should return the following results :
| age_cut | number |
|---|---|
| 25 Under the age of | 4 |
| 25 Years old and over | 3 |
/* select '25 Under the age of ' age_cut,sum(if(age >=25, 0, 1)) number from user_profile union select '25 Years old and over ' age_cut,sum(if(age >=25, 1, 0)) number from user_profile; select '25 Under the age of ' age_cut, count(*) number from user_profile where age < 25 or age is null union select '25 Years old and over ' age_cut, count(*) number from user_profile where age >=25; select if(age >=25, '25 Years old and over ', '25 Under the age of ') age_cut, count(*) number from user_profile group by age_cut; */
mysql> select if(age >=25, '25 Years old and over ', '25 Under the age of ') age_cut, count(*) number
-> from user_profile group by age_cut;
+-----------------+--------+
| age_cut | number |
+-----------------+--------+
| 25 Under the age of | 4 |
| 25 Years old and over | 3 |
+-----------------+--------+
2 rows in set (0.00 sec)
mysql> select '25 Under the age of ' age_cut,sum(if(age >=25, 0, 1)) number from user_profile
-> union
-> select '25 Years old and over ' age_cut,sum(if(age >=25, 1, 0)) number from user_profile;
+-----------------+--------+
| age_cut | number |
+-----------------+--------+
| 25 Under the age of | 4 |
| 25 Years old and over | 3 |
+-----------------+--------+
2 rows in set (0.00 sec)
mysql> select '25 Under the age of ' age_cut, count(*) number from user_profile where age < 25 or age is null
-> union
-> select '25 Years old and over ' age_cut, count(*) number from user_profile where age >=25;
+-----------------+--------+
| age_cut | number |
+-----------------+--------+
| 25 Under the age of | 4 |
| 25 Years old and over | 3 |
+-----------------+--------+
2 rows in set (0.00 sec)
27、CASE Use of functions
subject : Divide users into 20 Under the age of ,20-24 year ,25 Three age groups aged and over , View the details of users of different ages , Take out the corresponding data .( notes : If age is blank, return other )
Example :user_profile The data are as follows .
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | Fudan University | 4 | 15 | 5 | 25 | |
| 3 | 6543 | female | 20 | Peking University, | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | Shandong University | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | male | 26 | Fudan University | 3.6 | 9 | 6 | 52 |
The query should return the following results :
| device_id | gender | age_cut |
|---|---|---|
| 2138 | male | 20-24 year |
| 3214 | male | other |
| 6543 | female | 20-24 year |
| 2315 | female | 20-24 year |
| 5432 | male | 25 Years old and over |
| 2131 | male | 25 Years old and over |
| 4321 | male | 25 Years old and over |
answer :
/* select device_id, gender, case when age is null then ' other ' when age >=20 and age <= 24 then '20-24 year ' when age > 24 then '25 Years old and over ' end age_cut from user_profile; */
mysql> select device_id, gender,
-> case
-> when age is null then ' other '
-> when age >=20 and age <= 24 then '20-24 year '
-> when age > 24 then '25 Years old and over '
-> end age_cut
-> from user_profile;
+-----------+--------+----------------+
| device_id | gender | age_cut |
+-----------+--------+----------------+
| 2138 | male | 20-24 year |
| 3214 | male | other |
| 6543 | female | 20-24 year |
| 2315 | female | 20-24 year |
| 5432 | male | 25 Years old and over |
| 2131 | male | 25 Years old and over |
| 4321 | male | 25 Years old and over |
+-----------+--------+----------------+
7 rows in set (0.01 sec)
28、 Date function (YEAR) Use
subject : To calculate the 2021 year 8 Number of user practice questions per day per month , Take out the corresponding data .
Example :question_practice_detail The data are as follows .
| id | device_id | question_id | result | date |
|---|---|---|---|---|
| 1 | 2138 | 111 | wrong | 2021-05-03 |
| 2 | 3214 | 112 | wrong | 2021-05-09 |
| 3 | 3214 | 113 | wrong | 2021-06-15 |
| 4 | 6543 | 111 | right | 2021-08-13 |
| 5 | 2315 | 115 | right | 2021-08-13 |
| 6 | 2315 | 116 | right | 2021-08-14 |
| 7 | 2315 | 117 | wrong | 2021-08-15 |
| …… |
The query should return the following results :
| day | question_cnt |
|---|---|
| 13 | 5 |
| 14 | 2 |
| 15 | 3 |
| 16 | 1 |
| 18 | 1 |
Table structure and data are as follows :
/* CREATE TABLE `question_practice_detail` ( `id` int NOT NULL, `device_id` int NOT NULL, `question_id`int NOT NULL, `result` varchar(32) NOT NULL, `date` date NOT NULL ); INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03'); INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09'); INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15'); INSERT INTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14'); INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15'); INSERT INTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09'); INSERT INTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15'); INSERT INTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14'); INSERT INTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15'); INSERT INTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16'); INSERT INTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18'); INSERT INTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13'); */
answer :
/* select day(date) day, count(*) question_cnt from question_practice_detail where date between '2021-8-1' and '2021-8-31' group by day; */
mysql> select day(date) day, count(*) question_cnt
-> from question_practice_detail
-> where date between '2021-8-1' and '2021-8-31'
-> group by day;
+------+--------------+
| day | question_cnt |
+------+--------------+
| 13 | 5 |
| 14 | 2 |
| 15 | 3 |
| 16 | 1 |
| 18 | 1 |
+------+--------------+
5 rows in set (0.00 sec)
29、SUBSTRING_INDEX Use of functions (1)
subject : Count the number of contestants of users of each gender , Take out the corresponding results .
Example :user_submit The data are as follows .
| device_id | profile | blog_url |
|---|---|---|
| 2138 | 180cm,75kg,27,male | http:/url/bigboy777 |
| 3214 | 165cm,45kg,26,female | http:/url/kittycc |
| 6543 | 178cm,65kg,25,male | http:/url/tiger |
| 4321 | 171cm,55kg,23,female | http:/url/uhksd |
| 2131 | 168cm,45kg,22,female | http:/urlsydney |
The query should return the following results :
| gender | number |
|---|---|
| male | 2 |
| female | 3 |
Table structure and data are as follows :
/* drop table if exists user_submit; CREATE TABLE `user_submit` ( `id` int NOT NULL, `device_id` int NOT NULL, `profile` varchar(100) NOT NULL, `blog_url` varchar(100) NOT NULL ); INSERT INTO user_submit VALUES(1,2138,'180cm,75kg,27,male','http:/url/bisdgboy777'); INSERT INTO user_submit VALUES(1,3214,'165cm,45kg,26,female','http:/url/dkittycc'); INSERT INTO user_submit VALUES(1,6543,'178cm,65kg,25,male','http:/url/tigaer'); INSERT INTO user_submit VALUES(1,4321,'171cm,55kg,23,female','http:/url/uhsksd'); INSERT INTO user_submit VALUES(1,2131,'168cm,45kg,22,female','http:/url/sysdney'); */
answer :
/* select SUBSTRING_INDEX(profile,',',-1) gender, count(*) number from user_submit group by gender; */
mysql> select SUBSTRING_INDEX(profile,',',-1) gender, count(*) number
-> from user_submit
-> group by gender;
+--------+--------+
| gender | number |
+--------+--------+
| female | 3 |
| male | 2 |
+--------+--------+
2 rows in set (0.02 sec)
30、SUBSTRING_INDEX Use of functions (2)
subject :blog_url In the column url The string after the character is the user name of the user's personal blog , Take the user name as a new column , Take out the required data .
Example :user_submit The data are as follows .
| device_id | profile | blog_url |
|---|---|---|
| 2138 | 180cm,75kg,27,male | http:/ur/bisdgboy777 |
| 3214 | 165cm,45kg,26,female | http:/url/dkittycc |
| 6543 | 178cm,65kg,25,male | http:/ur/tigaer |
| 4321 | 171 cm,55kg,23,female | http:/url/uhksd |
| 2131 | 168cm,45kg,22,female | http:/url/sydney |
The query should return the following results :
| device_id | user_name |
|---|---|
| 2138 | bisdgboy777 |
| 3214 | dkittycc |
| 6543 | tigaer |
| 4321 | uhsksd |
| 2131 | sydney |
answer :
/* select device_id, SUBSTRING_INDEX(blog_url,'/',-1) user_name from user_submit; */
mysql> select device_id,
-> SUBSTRING_INDEX(blog_url,'/',-1) user_name
-> from user_submit;
+-----------+-------------+
| device_id | user_name |
+-----------+-------------+
| 2138 | bisdgboy777 |
| 3214 | dkittycc |
| 6543 | tigaer |
| 4321 | uhsksd |
| 2131 | sysdney |
+-----------+-------------+
5 rows in set (0.00 sec)
31、SUBSTRING_INDEX Use of functions (3)
subject : Count the number of contestants of users of each age , Take out the corresponding results .
Example :user_submit The data are as follows .
| device_id | profile | blog_url |
|---|---|---|
| 2138 | 180cm,75kg,27,male | http:/ur/bigboy777 |
| 3214 | 165cm,45kg,26,female | http:/url/kittycc |
| 6543 | 178cm,65kg,25,male | http:/url/tiger |
| 4321 | 171cm,55kg,23,female | http:/url/uhksd |
| 2131 | 168cm,45kg,22,female | http:/url/sydney |
The query should return the following results :
| age | number |
|---|---|
| 27 | 1 |
| 26 | 1 |
| 25 | 1 |
| 23 | 1 |
| 22 | 1 |
answer :
/* select SUBSTRING_INDEX(SUBSTRING_INDEX(profile,',',3),',',-1) age, count(*) number from user_submit group by age; */
mysql> select SUBSTRING_INDEX(SUBSTRING_INDEX(profile,',',3),',',-1) age,
-> count(*) number
-> from user_submit
-> group by age;
+------+--------+
| age | number |
+------+--------+
| 22 | 1 |
| 23 | 1 |
| 25 | 1 |
| 26 | 1 |
| 27 | 1 |
+------+--------+
5 rows in set (0.00 sec)
边栏推荐
- 《暗黑破坏神4》PS4/PS5测试版已加入PlayStation数据库
- 数据库系统原理与应用教程(058)—— MySQL 练习题(二):单选题
- 数据库系统原理与应用教程(059)—— MySQL 练习题:操作题 1-10(三)
- 不用Swagger,那我用啥?
- Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022
- 我秃了!唯一索引、普通索引我该选谁?
- Facial expression recognition based on pytorch convolution - graduation project "suggestions collection"
- 【架构】评分较高的三本微服务书籍的阅读笔记
- Lyscript get previous and next instructions
- PHP生成随机数(昵称随机生成器)
猜你喜欢

面经整理,助力秋招,祝你称为offer收割机

Have you seen the management area decoupling architecture? Can help customers solve big problems

Cool operation preheating! Code to achieve small planet effect

半波整流点亮LED

Excellent performance! Oxford, Shanghai, AI Lab, Hong Kong University, Shangtang, and Tsinghua have joined forces to propose a language aware visual transformer for reference image segmentation! Open

Org.apache.ibatis.exceptions.toomanyresultsexception

Leetcode-136. numbers that appear only once

Better and more modern terminal tools than xshell!

严格模式——let和const——箭头函数——解构赋值——字符串模板symbol——Set和Map——生成器函数

Half wave rectification light LED
随机推荐
什么叫杂谈(e网杂谈)
After finishing, help autumn move, I wish you call it an offer harvester
Strict mode -- let and const -- arrow function -- Deconstruction assignment -- string template symbol -- set and map -- generator function
Guide for using IP phone system and VoIP system
You have to apologize if you get involved in the funny shop?
Leetcdoe-342. Power of 4
vim常用命令详解(vim使用教程)
合并表格行---三层for循环遍历数据
用非递归的方法实现二叉树中的层遍历,先序遍历,中序遍历和后序遍历
C语言学生成绩管理系统详解[通俗易懂]
基于神经网络的帧内预测和变换核选择
Debezium series: major changes and new features of 2.0.0.beta1
Cool operation preheating! Code to achieve small planet effect
数据库系统原理与应用教程(061)—— MySQL 练习题:操作题 21-31(五)
I miss the year of "losing" Li Ziqi
[error] after logging in to another machine using SSH, you find that the hostname is still yourself | unable to access yarn8088
Basic exercises of DQL in MySQL
P1797重型运输 题解
使用 IPtables 进行 DDoS 保护
性能超群!牛津&上海AI Lab&港大&商汤&清华强强联手,提出用于引用图像分割的语言感知视觉Transformer!代码已开源...