数据库去重的sql语句
时间: 2023-06-21 14:10:12 浏览: 98
浅谈sql数据库去重
5星 · 资源好评率100%
以下是常用的数据库去重的 SQL 语句示例:
MySQL:
1. 通过 DISTINCT 去重
```
SELECT DISTINCT column1, column2, ...
FROM table_name;
```
2. 通过 GROUP BY 去重
```
SELECT column1, column2, ...
FROM table_name
GROUP BY column1, column2, ...;
```
3. 通过子查询去重
```
SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (
SELECT DISTINCT column_name
FROM table_name
);
```
Oracle:
1. 通过 DISTINCT 去重
```
SELECT DISTINCT column1, column2, ...
FROM table_name;
```
2. 通过 GROUP BY 去重
```
SELECT column1, column2, ...
FROM table_name
GROUP BY column1, column2, ...;
```
3. 通过 ROW_NUMBER() 去重
```
SELECT column1, column2, ...
FROM (
SELECT column1, column2, ..., ROW_NUMBER() OVER (PARTITION BY column_name ORDER BY column_name) rn
FROM table_name
)
WHERE rn = 1;
```
SQL Server:
1. 通过 DISTINCT 去重
```
SELECT DISTINCT column1, column2, ...
FROM table_name;
```
2. 通过 GROUP BY 去重
```
SELECT column1, column2, ...
FROM table_name
GROUP BY column1, column2, ...;
```
3. 通过 ROW_NUMBER() 去重
```
SELECT column1, column2, ...
FROM (
SELECT column1, column2, ..., ROW_NUMBER() OVER (PARTITION BY column_name ORDER BY column_name) rn
FROM table_name
)
WHERE rn = 1;
```
注意:以上示例仅供参考,具体的语法和实现可能会因数据库类型、版本和数据结构而有所不同。
阅读全文