在mysql資料庫中如何讓某個欄位有重複的只取一條

2021-03-29 00:25:00 字數 3634 閱讀 4950

1樓:

-- 保留相

同a值的最小id行

select *

from table_a a

where not exists (

select 1 from table_a bwhere b.a = a.a

and b.id < a.id)

2樓:草堂春日遲遲

select * from table_a where id in (select min(id) from table_a group by a)

3樓:匿名使用者

我知道oracle有個row_number()函式可以實現某個欄位排序,然後取重複的一條,下面的部落格連結是mysql實現oracle的row_number()函式功能,看看有沒有啟發吧。

4樓:破玩意真難取

可以試試distinct

5樓:都比劉先生

select * from table_a group by a order by id desc;

sql根據某一個欄位重複只取第一條資料

6樓:

使用分析函式row_number() over (partiion by ... order by ...)來進行分組編號,然後取分組標號值為1的記錄即可。

目前主流的資料庫都有支援分析函式,很好用。

其中,partition by 是指定按哪些欄位進行分組,這些欄位值相同的記錄將在一起編號;order by則是指定在同一組中進行編號時是按照怎樣的順序。

示例(sql server 2005或以上適用):

select s.*

from (

select *, row_number() over (partition by [手機號] order by [店鋪]) as group_idx

from table_name

) swhere s.group_idx = 1

7樓:匿名使用者

用group by 最後一個欄位 用個max()

8樓:發生等將發生

如果僅僅只是查詢出來去從,那麼就用distinctselect distinct 需要去重的列明(允許多列) from table

如果是需要在表中刪除,可以這樣處理

1、建立臨時表,將重複記錄查詢出來去重插入到臨時表2、刪除實表中的重複記錄

3、將臨時表中的記錄插入到實表

處理完成

9樓:匿名使用者

select * into ##tmp_table from 表where 1=2

declare @phoneno int

declare cur cursor forselect 手機號 from 表 group by 手機號open cur

fetch next from cur into @phonenowhile @@fetch_status=0begin

insert into ##tmp_tableselect top 1 from 表 where 手機號=@phoneno

fetch next from cur into @phonenoendselect * from ##tmp_tabledrop table ##tmp_table

10樓:匿名使用者

最簡單的 select distinct (手機號)

11樓:老漢肆

select

temp.id,

temp.device_id,

temp.update_dtm,

temp.test_result

from (

select

t.id,

t.device_id,

t.update_dtm,

t.test_result,

row_number() over(partition by device_id order by t.update_dtm desc) as row_***

from device_info_tbl t ) tempwhere temp.row_*** = '1'

求一mysql語句,去掉資料庫裡某個欄位相同記錄,只保留一個。

12樓:

select distinct(欄位名) from 表名

13樓:匿名使用者

你舉個簡單例子吧,這樣別人容易懂你的問題,問題應該不難

14樓:匿名使用者

用gorup by 你的欄位

mysql查詢結果中有多條重複記錄只保留一條

15樓:匿名使用者

這個需要分情況。 1,你的資料庫表中有主鍵,且主鍵上面的資料為唯一值。也就是沒有重複值。 那麼你在刪除的時候,將這個唯一值作為條件進行刪除。

16樓:匿名使用者

如果是完全重複的話可以用distinct關鍵字去重。

在mysql資料庫中如何讓某個欄位有重複的只取一條

17樓:魚尾摯愛白菜

select *

from table  ###

where not exists (

select * from table  ###where # = #

and ## < ##

)在使用mysql時,有時需要查詢出某個欄位不重複的記錄,雖然mysql提供 有distinct這個關鍵字來過濾掉多餘的重複記錄只保留一條,但往往只用它來返回不重複記錄的條數,而不是用它來返回不重記錄的所有值。其原因是 distinct只能返回它的目標欄位,而無法返回其它欄位,這個問題讓我困擾了很久,用distinct不能解決的話,只有用二重迴圈查詢來解決。

給個例子把,比如:表table_a 4條資料id a b c d

01 ab 1a2 1b2 121

02 ab 2a3 3b3 4a1

03 ac 1a2 1b2 121

04 ac 2a4 3b2 52g

何讓a欄位重複取條 比

01 ab 1a2 1b2 121

03 ac 1a2 1b2 121

保留相同a值id行

select *

from table_a a

where not exists (

select 1 from table_a bwhere b.a = a.a

and b.id < a.id)

18樓:匿名使用者

select max(id) as id,fid,title,date from table group by fid,title,date

19樓:尋_常

select * from (select * from a order by id desc) as b group by fid

如何在MYSQL資料庫中資料庫,如何在MYSQL資料庫中新建一個資料庫

createusertomidentifiedby 密碼 建立使用者 grantallprivilegesondbname.totom 把dbname庫的所有操作許可權都給tomflushprivileges 重新整理快取,生效 在控制檯根目錄下開啟sqlserver企業管理器,新建sqlserve...

mysql把資料庫中的資料複製到另資料庫中的表表結構相同

1。表結構相同的表,且在同一資料庫 如,table1,table2 sql insert into table1 select from table2 完全複製 insert into table1 select distinct from table2 不復制重複紀錄 insert into ta...

mysql資料庫和access資料庫有什麼區別

mysql和access的區別 mysql特性 很便宜,通常是免費的 網路承載少 查詢 優化 可以簡便的應用程式通過mysql做備份 mysql操縱簡單,易上手,且為各種不同的資料格式提供有彈性的擴充套件介面 odbc access特性 簡單易學,使用方便,開發效率高。mysql對於大多數使用者而言...