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

2021-05-29 09:35:46 字數 4025 閱讀 8622

1樓:小小筱冉絲

-- 保留

bai相du

同zhia值的

dao最小

專屬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 top 1 id,name,age from 表 order by age desc按照年齡倒序抄排序,然後取第一條bai。考慮可能有多人年齡相

du同,如果都zhi需取出,可dao以這樣寫:select id,name,age from 表 where age=(select max(age) from 表)

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

3樓:

使用分析函式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

4樓:匿名使用者

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

5樓:發生等將發生

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

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

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

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

處理完成

6樓:匿名使用者

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

7樓:匿名使用者

最簡單的 select distinct (手機號)

8樓:老漢肆

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資料庫中如何讓某個欄位有重複的只取一條 5

9樓:

-- 保留相

同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)

10樓:草堂春日遲遲

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

11樓:匿名使用者

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

12樓:破玩意真難取

可以試試distinct

13樓:都比劉先生

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

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

14樓:魚尾摯愛白菜

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)

15樓:匿名使用者

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

16樓:尋_常

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

mysql資料庫中多條重複資料,如何只刪除一條?

17樓:薔薇夜天涯

這個需要分情況。

1,你的資料庫表中有主鍵,且主鍵上面的資料為唯一值。也就是沒有重專復值。

那麼你在刪除的時屬候,將這個唯一值作為條件進行刪除。

如: delete from [表名] where id=12.所有的資料相同,那麼你只能開啟資料表,手工選定其中某一條,進行刪除。

18樓:匿名使用者

沒有主鍵你就給它加一個,問題就容易解決了。

19樓:挑不死

刪除語句後面加limit 1

為什麼mysql資料庫安,mysql資料庫裝不上什麼問題?

mysql5.7.21安裝 mysql資料庫裝不上什麼問題?肯定是上次沒有完全解除安裝 先把你裝的解除安裝了 然後去 documents and settings裡找有關和mysql有關的檔案然後全部刪掉 在安裝就行了 在什麼系統上安裝?出了什麼錯誤資訊?錯誤資訊貼上出來 為什麼我的mysql資料庫...

mysql修改欄位內容,mysql資料庫中sql修改欄位型別要怎麼做?

update banji set num 要改的值 update 表名稱 set 列名稱 新值 where 列名稱 某值 如果要改全部,就忽略條件.mysql資料庫中sql修改欄位型別要怎麼做?方法如下 復 修改一個字制 段的型別 alter table user modify new1 varch...

phpmyadmin的mysql資料庫中主鍵索引唯一各

主鍵 是唯一標示當前表裡的那一列,用主鍵可以代替表裡一條實體的內容,如 你有一個名字,有你的住址,號碼,籍貫,學位,政治面貌,等屬性,但唯一能夠標示你的還是你的身份證號,因為別人有可能有跟你同樣的名字,或者同樣的住址,或者同樣的的學位。但身份證是唯一的。通過身份證,我完全知道你本人。索引 就相當於你...