当前位置:网站首页>Task06: Autumn move script B

Task06: Autumn move script B

2022-06-10 08:49:00 JxWang05

Tutorial address

https://github.com/datawhalechina/wonderful-sql
https://gitee.com/datawhalechina/wonderful-sql

1. Transfer line column

Build table

mysql> DROP TABLE IF EXISTS exercise;
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE TABLE IF NOT EXISTS exercise
    -> ( index_id INTEGER PRIMARY KEY,
    ->   name VARCHAR(5),
    ->   subject VARCHAR(10),
    ->   score INTEGER
    -> );
Query OK, 0 rows affected (0.04 sec)
mysql> INSERT INTO exercise VALUES 
    ->  (1, 'A', 'chinese', 99),
    ->  (2, 'A', 'math', 98),
    ->  (3, 'A', 'english', 97),
    ->  (4, 'B', 'chinese', 92),
    ->  (5, 'B', 'math', 91),
    ->  (6, 'B', 'english', 90),
    ->  (7, 'C', 'chinese', 88),
    ->  (8, 'C', 'math', 87),
    ->  (9, 'C', 'english', 86)
    -> ;
Query OK, 9 rows affected (0.01 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM exercise;
+----------+------+---------+-------+
| index_id | name | subject | score |
+----------+------+---------+-------+
|        1 | A    | chinese |    99 |
|        2 | A    | math    |    98 |
|        3 | A    | english |    97 |
|        4 | B    | chinese |    92 |
|        5 | B    | math    |    91 |
|        6 | B    | english |    90 |
|        7 | C    | chinese |    88 |
|        8 | C    | math    |    87 |
|        9 | C    | english |    86 |
+----------+------+---------+-------+
9 rows in set (0.01 sec)

Inquire about

mysql> SELECT
    ->  name,
    ->  SUM(CASE WHEN subject = 'chinese' THEN score ELSE NULL END) AS chinese,
    ->  SUM(CASE WHEN subject = 'math' THEN score ELSE NULL END) AS math,
    ->  SUM(CASE WHEN subject = 'english' THEN score ELSE NULL END) AS english
    -> FROM exercise
    -> GROUP BY name
    -> ;
+------+---------+------+---------+
| name | chinese | math | english |
+------+---------+------+---------+
| A    |      99 |   98 |      97 |
| B    |      92 |   91 |      90 |
| C    |      88 |   87 |      86 |
+------+---------+------+---------+
3 rows in set (0.00 sec)

This is in the previous tutorial , I didn't take a close look at it , Now I'm really confused , Then go back and see

We use it case after , The qualified line is the original value , What doesn't fit is null

Then you need to use sum Merge into one line , This is convenient in group Then it shows

That is to say , It is similar to grouping and summing qualified data

2. Column turned

Build table

mysql> DROP TABLE IF EXISTS exercise;
Query OK, 0 rows affected (0.04 sec)

mysql> CREATE TABLE IF NOT EXISTS exercise
    -> ( index_id INTEGER PRIMARY KEY,
    ->   name VARCHAR(5),
    ->   chinese INTEGER,
    ->   math INTEGER,
    ->   english INTEGER
    -> );
Query OK, 0 rows affected (0.04 sec)

mysql> INSERT INTO exercise VALUES 
    ->  (1, 'A', 99, 98, 97),
    ->  (2, 'B', 92, 91, 90),
    ->  (3, 'C', 88, 87, 86)
    -> ;
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM exercise;
+----------+------+---------+------+---------+
| index_id | name | chinese | math | english |
+----------+------+---------+------+---------+
|        1 | A    |      99 |   98 |      97 |
|        2 | B    |      92 |   91 |      90 |
|        3 | C    |      88 |   87 |      86 |
+----------+------+---------+------+---------+
3 rows in set (0.00 sec)

Inquire about

mysql> SELECT
    ->  *
    -> FROM (
    ->  SELECT name, 'chinese' AS subject, chinese AS score FROM exercise
    ->  UNION
    ->  SELECT name, 'math' AS subject, math AS score FROM exercise
    ->  UNION
    ->  SELECT name, 'english' AS subject, english AS score FROM exercise
    -> ) AS ori
    -> ORDER BY ori.name
    -> ;
+------+---------+-------+
| name | subject | score |
+------+---------+-------+
| A    | chinese |    99 |
| A    | math    |    98 |
| A    | english |    97 |
| B    | chinese |    92 |
| B    | math    |    91 |
| B    | english |    90 |
| C    | chinese |    88 |
| C    | math    |    87 |
| C    | english |    86 |
+------+---------+-------+
9 rows in set (0.00 sec)

What I wanted to use was a connection , Later, it was found that the connections were horizontal

Vertical connection , Sure enough, I still need to use union

In order to be consistent with the results , We set up a query outside to sort

3. Who is the star anchor

Build table

mysql> DROP TABLE IF EXISTS exercise;
Query OK, 0 rows affected (0.04 sec)

mysql> CREATE TABLE IF NOT EXISTS exercise
    -> ( index_id INTEGER PRIMARY KEY,
    ->   anchor_name VARCHAR(5),
    ->   date VARCHAR(10),
    ->   sales INTEGER
    -> );
Query OK, 0 rows affected (0.04 sec)
mysql> INSERT INTO exercise VALUES 
    ->  (1, 'A', '20210101', 40000),
    ->  (2, 'B', '20210101', 80000),
    ->  (3, 'A', '20210102', 10000),
    ->  (4, 'C', '20210102', 90000),
    ->  (5, 'A', '20210103', 7500),
    ->  (6, 'C', '20210103', 80000)
    -> ;
Query OK, 6 rows affected (0.02 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM exercise;
+----------+-------------+----------+-------+
| index_id | anchor_name | date     | sales |
+----------+-------------+----------+-------+
|        1 | A           | 20210101 | 40000 |
|        2 | B           | 20210101 | 80000 |
|        3 | A           | 20210102 | 10000 |
|        4 | C           | 20210102 | 90000 |
|        5 | A           | 20210103 |  7500 |
|        6 | C           | 20210103 | 80000 |
+----------+-------------+----------+-------+
6 rows in set (0.00 sec)

Inquire about

mysql> SELECT
    ->  *
    ->  -- COUNT(DISTINCT anchor_name) AS cnt_anchor,
    ->  -- COUNT(DISTINCT date) AS cnt_date
    -> FROM
    ->  exercise AS o2
    -> WHERE sales >= 0.9 * (
    ->  SELECT
    ->   SUM(sales)
    ->  FROM exercise AS o1
    ->  WHERE o1.date = o2.date
    ->  GROUP BY date
    -> )
    -> ;
+----------+-------------+----------+-------+
| index_id | anchor_name | date     | sales |
+----------+-------------+----------+-------+
|        4 | C           | 20210102 | 90000 |
|        6 | C           | 20210103 | 80000 |
+----------+-------------+----------+-------+
2 rows in set (0.00 sec)

mysql> SELECT
    ->  COUNT(DISTINCT anchor_name) AS cnt_anchor,
    ->  COUNT(DISTINCT date) AS cnt_date
    -> FROM
    ->  exercise AS o2
    -> WHERE sales >= 0.9 * (
    ->  SELECT
    ->   SUM(sales)
    ->  FROM exercise AS o1
    ->  WHERE o1.date = o2.date
    ->  GROUP BY date
    -> )
    -> ;
+------------+----------+
| cnt_anchor | cnt_date |
+------------+----------+
|          1 |        2 |
+------------+----------+
1 row in set (0.00 sec)

The condition is , A certain anchor's sales account for 90% And above

So first of all , We have to follow date grouping , Find out the total sales per day

Then use the associated subquery , Proportion obtained 90% And above

Then redo the anchor list , Count , Is the star anchor

De duplicate the date , Count , It's star anchor day

4. MySQL How to view sql Statement execution plan ? What information can you see ?

I really forgot this , After checking, I found that it was explain

mysql> explain select * from exercise;
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------+
|  1 | SIMPLE      | exercise | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    6 |   100.00 | NULL  |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)

5. Explain it. SQL In the database ACID What is it?

Probably know that it is atomic 、 Uniformity 、 persistence , Another one forgot

Atomicity (Atomicity): All operations in a transaction as a whole are as inseparable as atoms , All or nothing , All or nothing .
Uniformity (Consistency): The execution result of the transaction must make the database from one consistency state to another . A state of consistency means :1. The state of the system satisfies the data integrity constraints ( Main code , Referential integrity ,check Constraints etc. ) 2. The state response database of the system should describe the real state of the real world , For example, the sum of the two accounts before and after the transfer should remain the same .
Isolation, (Isolation): Concurrent transactions do not affect each other , Its impact on the database is the same as when they are executed serially . For example, multiple users transfer money to one account at the same time , The result of the final account should be the same as the result of their transfer in sequence .
persistence (Durability): Once the transaction is committed , Its update to the database is persistent . No transaction or system failure will result in data loss .4

Here comes the blog I wrote before :https://blog.csdn.net/weixin_52202311/article/details/121871789

I found that this seems to be used to describe transactions

原网站

版权声明
本文为[JxWang05]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100834330840.html