当前位置:网站首页>Task06: Autumn move script B
Task06: Autumn move script B
2022-06-10 08:49:00 【JxWang05】
Task06: Autumn move script B
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
边栏推荐
- Video Downloader: the latest download of 4K Video Downloader
- [JUC series] basic use of thread pool
- vtk学习之引用计数与智能指针
- Why is Dao voting platform snapshot worth paying attention to?
- R language uses the Mhor function of epidisplay package to perform Cochran mantel Haenszel test, visualize and test whether the two classification variables are independent when the third variable is
- HarmonyOS(鸿蒙)全网最全资源汇总,吐血整理,赶紧收藏!
- Test: Cup
- Simple operation and debugging of GPIO in Qualcomm platform
- MMSegmention系列之三(基本的网络架构和预训练模型)
- Tenants roaming the rental complex
猜你喜欢

Texture mapping for VTK learning

Take stock of the tourist attractions in Singapore

Computer level 2 test preparation MySQL day 4
![Huawei software test interview question | a Huawei successful employee's sharing [written examination question]](/img/4c/7fb9390dd9490c6a1a75331e737d97.jpg)
Huawei software test interview question | a Huawei successful employee's sharing [written examination question]

想转行,为什么首选软件测试?
![[cryptography] AES encryption and decryption](/img/a5/ad3fed3004646ca894d59cc22d2f11.png)
[cryptography] AES encryption and decryption

Google搜索为什么不能无限分页?

win11下配置vscode+cmake

乐鑫对 Zephyr 的最新支持

wechat_ Configuration of wechat applet subcontracting
随机推荐
MMSegmention系列之五(自定义模型)
对线HR_MySQL逻辑架构?就这?
[cryptography] AES encryption and decryption
USB TYPE -A -B -C 接口
wechat_微信小程序分包的配置
R语言使用epiDisplay包的pyramid函数可视化金字塔图
Test: Cup
Harmonyos (Hongmeng) collects the most complete resources in the whole network, sorts out hematemesis, and collects them quickly!
Oracle SQL command line (II. View (2))
win11下配置vscode+cmake
JS obtain the date of birth, gender and age through ID number
Task04:集合运算
Latex基本语法备注
USB type -a -b -c interface
How to hide application previews when switching applications while using shutter
Video Downloader: the latest download of 4K Video Downloader
RunLoop的实际使用
texstudio 显示行号和不检查拼写设置
How far is your team from continuous deployment in 2022?
对线HR_MySQL存储引擎,原来是这样啊