当前位置:网站首页>MySQL 常用命令
MySQL 常用命令
2022-07-23 09:39:00 【云胡同学】
一、概述
总结一些常用的命令
二、命令
2.1 复制旧表到新表
CREATE TABLE new_table_name
SELECT * FROM old_table_name;
2.2 删除表中数据
delete from table_name;
2.3 给结果添加编号
SET @row_number = 0;
SELECT (@row_number := @row_number + 1 ) AS num
FROM table_name
2.4 查询分组的最小 id
SELECT min(book_id), book_name, book_type
FROM t_book
GROUP BY book_name, book_type # 根据两个字段进行分组
2.5 删除重复记录,保留最小索引的那一个
t_test 表中数据:
| id | book_name | book_type |
|---|---|---|
| 1 | 失败者的春秋 | noval |
| 2 | 战国歧途 | noval |
| 3 | 失败者的春秋 | noval |
| 4 | 战国歧途 | noval |
| 5 | 失败者的春秋 | essay |
重复记录用两个字段进行唯一确定。
因此 id 为 1 的记录与 id 为 3 的记录是重复记录,与 id 为 5 的记录不是重复记录,因为他们的 book_type 不同, 一个是 noval,一个是 essay。
因此我们想得到的结果是删除 3、4 两行记录,他们重复了。
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;
- 先获取分组后的最小的 id,
- 删除重复记录并且这个 id 不能是最小的 id,因此就保留下最小的 id 的记录。
结果:
| id | book_name | book_type |
|---|---|---|
| 1 | 失败者的春秋 | noval |
| 2 | 战国歧途 | noval |
| 5 | 失败者的春秋 | essay |
边栏推荐
- Jetpack系列之Room中存Map结构
- 自研的数据产品迭代了一年多,为什么不买第三方商业数据平台产品呢?
- 【软件测试】如何梳理你测试的业务
- Some libraries that can perform 2D or 3D triangulation
- After using vscode to format the code, save and find that the code is messy again. What should I do? Vs remove formatting
- @FeignClient使用详细教程(图解)
- PKI体系快速介绍
- Zhongwang CAD professional 2022 software installation package download and installation tutorial
- 微信官方出品!小程序自动化框架 minium 分享预告
- C# 线程锁和单多线程简单使用
猜你喜欢

mysql唯一索引无重复值报错重复

Using JS to parse and execute XSS automatically

OpenHarmony南向学习笔记——Hi3861+HC-SR04超声波检测

【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显

leetcode: 17. 电话号码的字母组合

微信官方出品!小程序自动化框架 minium 分享预告

【面试高频】cookie、session、token?看完再也不担心被问了

R language practical application case: drawing part (III) - drawing of multiple combination patterns

深度学习单图三维人脸重建

什么是Per-Title编码?
随机推荐
mysql函数汇总之数学函数
Authing supports Zadig! Unified authentication and rapid docking of cloud native users
About flex layout justify content: the last solution to the misalignment of space around and why it is solved like this is a discussion
反射调用事务方法导致事务失效
利用shell脚本实现封禁扫描频率过高的ip
@FeignClient使用詳細教程(圖解)
【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显
Kettle implémente une connexion de base de données partagée et insère une instance de composant de mise à jour
一道代码题看 CommonJS 模块化规范
Linux scheduled database backup script
俄方希望有效落实农产品外运“一揽子”协议
What methods are called behind the use of objects
Quick introduction to PKI system
Jetpack系列之Room中存Map结构
Right click to create a new TXT. The new text file is missing. You can solve it by adding a registry. Find the ultimate solution that can't be solved
C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)
Kettle實現共享數據庫連接及插入更新組件實例
Openharmony South learning notes - hi3861+hc-sr04 ultrasonic testing
【软件测试】如何梳理你测试的业务
Detailed tutorial of typora drawing bed configuration