SQL DISTINCT 关键字与SELECT语句结合使用,以消除所有重复记录并仅获取唯一记录。
Distinct - 语法
消除重复记录的DISTINCT关键字的基本语法如下-
SELECT DISTINCT column1, column2,.....columnN FROM table_name WHERE [condition]
Distinct - 示例
考虑具有以下记录的CUSTOMERS表-
+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Learnfk | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
首先,让无涯教程看看下面的SELECT查询如何返回重复的薪水记录。
SQL> SELECT SALARY FROM CUSTOMERS ORDER BY SALARY;
这将产生以下输出,其中薪水(2000)两次出现,这是原始表中的重复记录。
+----------+ | SALARY | +----------+ | 1500.00 | | 2000.00 | | 2000.00 | | 4500.00 | | 6500.00 | | 8500.00 | | 10000.00 | +----------+
现在,让无涯教程在上面的SELECT查询中使用DISTINCT关键字,然后查看输出。
SQL> SELECT DISTINCT SALARY FROM CUSTOMERS ORDER BY SALARY;
如果没有任何重复的条目,这将产生以下输出。
+----------+ | SALARY | +----------+ | 1500.00 | | 2000.00 | | 4500.00 | | 6500.00 | | 8500.00 | | 10000.00 | +----------+
参考链接
https://www.learnfk.com/sql/sql-distinct-keyword.html
标签:SALARY,Distinct,2000.00,无涯,DISTINCT,+----------+,SQL,SELECT From: https://blog.51cto.com/u_14033984/9259312