1. 程式人生 > >檢索出某個欄位重複(或不重複)記錄的SQL

檢索出某個欄位重複(或不重複)記錄的SQL

1.表Information中有四個欄位:ID,Name,Sex,Position ID為主鍵,現在想查詢姓名一樣的記錄,sql語句應該寫為:

select *
from Information
where Name in (
select Name from Information group by name having count(*)>1)  

2.如果要查詢姓名不同的記錄,sql語句應該寫為:

select *
from Information
where Name   not   in (
select Name from Information group by name having count(*)>1)  

3.如果是查詢記錄數,只要將上述語句中的* 改為count(*)或count(Name),其中count(Name)比count(*)的效率要高.