3.select_distinct_不重復字段組合的數據


USE SQLSERVER;
SELECT * FROM EMP;

SELECT DISTINCT MGR FROM EMP;
SELECT DISTINCT DEPTNO FROM EMP;
select distinct comm from emp;

select distinct comm, deptno from emp;
--查詢不重復的值
--distinct關鍵字

select * from emp;

select distinct deptno from emp;

select distinct deptno as "等級" from emp;

select distinct sal from emp;

select distinct job from emp;

--NULL也是一個值,查詢唯一時也把NULL算入
select distinct comm from emp;

select distinct deptno, comm from emp;
select distinct comm, deptno from emp;

select distinct job, ename from emp;

--錯誤的寫法
select sal, distinct deptno from emp;
--distinct使用來修飾select關鍵字的,不是用來修飾字段的

select distinct sal, distinct deptno from emp;
--distinct使用來修飾select關鍵字的,不是用來修飾字段的

/*
注意:
distinct 修飾的是 select

單個字段:select distinct 查詢為單個字段中不重復的
多個字段:select distinct F1, F2, F3... 查詢為字段組(所有字段)中不重復的
語法:
select distinct F1, F2, F3... (單|多字段)from table;

例如:
select distinct deptno, comm from emp
來自 tmp表 查詢 deptno, comm組合起來不包含重復數據 

語意:
來自table中查詢的字段不包含重復數據(單|多字段)的組合
*/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM