当前位置:网站首页>MySQL common commands
MySQL common commands
2022-07-24 00:33:00 【Yun Hu】
One 、 summary
Summarize some common commands
Two 、 command
2.1 Copy old table to new table
CREATE TABLE new_table_name
SELECT * FROM old_table_name;
2.2 Delete data in table
delete from table_name;
2.3 Add a number to the result
SET @row_number = 0;
SELECT (@row_number := @row_number + 1 ) AS num
FROM table_name
2.4 The minimum number of query groups id
SELECT min(book_id), book_name, book_type
FROM t_book
GROUP BY book_name, book_type # Group according to two fields
2.5 Delete duplicate records , The one that retains the smallest index
t_test The data in the table :
| id | book_name | book_type |
|---|---|---|
| 1 | The age of losers | noval |
| 2 | Warring States astray | noval |
| 3 | The age of losers | noval |
| 4 | Warring States astray | noval |
| 5 | The age of losers | essay |
Duplicate records are uniquely identified by two fields .
therefore id by 1 The record of and id by 3 The record of is a duplicate record , And id by 5 The record of is not a duplicate record , Because of their book_type Different , One is noval, One is essay.
So what we want is to delete 3、4 Two lines of records , They repeated .
DELETE
FROM t_test
WHERE (book_name, book_type) IN (
SELECT *
FROM (SELECT book_name, book_type
FROM t_test
GROUP BY book_name, book_type
HAVING count(*) > 1) A)
AND id NOT IN (
SELECT *
FROM (SELECT min(id)
FROM t_test
GROUP BY book_name, book_type
HAVING count(*) > 1) B)
ORDER BY book_name, book_type;
- Get the smallest after grouping first id,
- Delete duplicate records and this id It cannot be the smallest id, So keep the smallest id The record of .
result :
| id | book_name | book_type |
|---|---|---|
| 1 | The age of losers | noval |
| 2 | Warring States astray | noval |
| 5 | The age of losers | essay |
边栏推荐
- English grammar_ Demonstrative pronoun -such / the same
- MySQL table field quantity limit and row size limit
- How to improve data quality
- 数据模型设计方法概述
- How can dbcontext support the migration of different databases in efcore advanced SaaS system
- AWS Article 3 how to publish message to IOT mqtt in go language
- Gbase 8C session information function (V)
- English语法_指示代词 -such / the same
- Classic example of C language - loan balance
- GBase 8c 访问权限访问函数(四)
猜你喜欢

数仓数据标准详解-2022

数仓数据指标和标签体系区别

PHP implements stripe subscription

Printf function - conversion description

XXL job realizes the code parsing of email sending warnings (line by line code interpretation)

High number_ Chapter 1 space analytic geometry and vector algebra__ Two point distance

Robot dog back submachine gun shooting video fire, netizens shivering: stoooooooopppp!

Application of SCA on devsecops platform

理解多态,让不同的“人”做同一件事情会产生不同的结果

How to realize 485 wireless communication between multiple sensors and Siemens PLC?
随机推荐
GBase 8c模式可见性查询函数(二)
Codeforces Round #807 (Div. 2)(A-D)
Gbase 8C access authority query function (I)
GBase 8c 位串操作符(一)
AWS Part 4 one machine and one secret
GBase 8c 访问权限查询函数(二)
通信模块整理(二)HC-05
Gbase 8C system table information function (II)
Pytest interface automated testing framework | how to get help
Application of SCA on devsecops platform
High number_ Chapter 2 differential calculus of multivariate functions__ Geometric application of partial derivatives_ Tangent and normal plane of space curve
Gbase 8C system table information function (III)
There are various signs that apple is expected to support AV1
泛型机制和增强for循环
Expérience du système réseau: résoudre les problèmes de ping
Educational Codeforces Round 132 (Rated for Div. 2)(A-D)
Gbase 8C access authority query function (II)
Redis主从同步机制
GBase 8c 访问权限查询函数(三)
北峰通信亮相中国(厦门)应急展|智能化通信手段强势吸睛!