可以看到表中的value字段有重復(fù),如果想篩選去重,使用select distinct語(yǔ)句如下:

目前成都創(chuàng)新互聯(lián)公司已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、阿瓦提網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
得到結(jié)果會(huì)是
| value
| a
| b
| c
| e
| f
篩選去重是實(shí)現(xiàn)了,可是只有選中的value列顯示了出來(lái),如果我想知道對(duì)應(yīng)的id呢?
嘗試一下把id字段加入sql語(yǔ)句,如下:
得到結(jié)果:
| value | id
| a | 1
| b | 2
| c | 3
| c | 4
| e | 5
| f | 5
更換一下sql語(yǔ)句中id和value的順序,如下:
得到結(jié)果:
| id |value
| 1 | a
| 2 | b
| 3 | c
| 4 | c
| 5 | e
| 5 | f
好像看明白它的作用結(jié)果了,只有id和value兩個(gè)字段同時(shí)重復(fù)時(shí),select distinct語(yǔ)句才會(huì)把它列入“去重”清單
所以能看到id為3和4的value雖然都是4,但由于select語(yǔ)句中寫了id字段,它也默認(rèn)會(huì)對(duì)id字段起效。
而且如果sql語(yǔ)句中把DISTINCT放到只想起效的字段前,那也是不行的....比如sql語(yǔ)句改為:
會(huì)提示sql報(bào)錯(cuò)。
那到底怎么樣能得到我想要的只對(duì)value字段內(nèi)容去重,顯示結(jié)果又能保留其他字段內(nèi)容呢....
找到的解決方法是使用group by函數(shù),sql語(yǔ)句如下:
得到結(jié)果:
| min(id) |value
| 1 | a
| 2 | b
| 3 | c
| 5 | e
| 5 | f
完成目標(biāo)了?!
如果把sql語(yǔ)句中的min()換成max()呢?
得到結(jié)果:
| min(id) |value
| 1 | a
| 2 | b
| 4 | c
| 5 | e
| 5 | f
也完成目標(biāo)了?!
同時(shí)比對(duì)兩次sql運(yùn)行結(jié)果可以發(fā)現(xiàn),
第一次使用min(id)時(shí),由于重復(fù)結(jié)果存在兩條而id最小的為為3,符合min(id)的篩選條件,所以結(jié)果中把id等于4的重復(fù)記錄刪除了。
第二次使用max(id)時(shí)結(jié)果中,也就把id等于3的重復(fù)記錄刪除了
可以推論到假如還存在一條id=5,value=c的記錄,使用max(id)時(shí)得到的結(jié)果里就會(huì)是5 c這條了。
再來(lái)嘗試一下,如果min()和max()用在value字段里呢:
得到結(jié)果:
| id |min(value)
| 1 | a
| 2 | b
| 3 | c
| 4 | c
| 5 | e
得到結(jié)果:
| id |min(value)
| 1 | a
| 2 | b
| 3 | c
| 4 | c
| 5 | f
再仔細(xì)想想,這種需求也只出現(xiàn)在不是那么care顯示結(jié)果中,非去重目標(biāo)字段的內(nèi)容時(shí)才能使用,如果需要指定這些字段的值,可能篩選條件就不是min()和max()那么簡(jiǎn)單了....
以上。
-------------------部分字段重復(fù)---------------------
--1.加索引的方式
create table test_2(id int,value int);
insert test_2 select 1,2 union all select 1,3 union all select 2,3;
Alter IGNORE table test_2 add primary key(id);
select * from test_2;
+----+-------+
| id | value |
+----+-------+
| 1 | 2 |
| 2 | 3 |
+----+-------+
我們可以看到 1 3 這條記錄消失了
我們這里也可以使用Unique約束 因?yàn)橛锌赡芰兄杏蠳ULL值,但是這里NULL就可以多個(gè)了..
--2.聯(lián)合表刪除
create table test_2(id int,value int);
insert test_2 select 1,2 union all select 1,3 union all select 2,3;
delete A from test_2 a join (select MAX(value) as v ,ID from test_2 group by id) b
on a.id=b.id and a.valueb.v;
select * from test_2;
+------+-------+
| id | value |
+------+-------+
| 1 | 3 |
| 2 | 3 |
+------+-------+
--3.使用Increment_auto也可以就是上面全部字段去重的第二個(gè)方法
--4.容易錯(cuò)誤的方法
--有些朋友可能會(huì)想到子查詢的方法,我們來(lái)試驗(yàn)一下
create table test_2(id int,value int);
insert test_2 select 1,2 union all select 1,3 union all select 2,3;
delete a from test_2 a where exists(select * from test_2 where a.id=id and a.valuevalue);
/*ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause*/
目前,您不能從一個(gè)表中刪除,同時(shí)又在子查詢中從同一個(gè)表中選擇。
不建議直接刪除,養(yǎng)成良好的習(xí)慣(刪除更麻煩),以下是將去重后的數(shù)據(jù)轉(zhuǎn)移到另一張表代碼:
Insert into 表名(列名)select distinct 列名 from 表名
你可以按照去重的思路,刪除重復(fù)數(shù)據(jù)
MySQL 刪除重復(fù)數(shù)據(jù)
有些 MySQL 數(shù)據(jù)表中可能存在重復(fù)的記錄,有些情況我們?cè)试S重復(fù)數(shù)據(jù)的存在,但有時(shí)候我們也需要?jiǎng)h除這些重復(fù)的數(shù)據(jù)。
本章節(jié)我們將為大家介紹如何防止數(shù)據(jù)表出現(xiàn)重復(fù)數(shù)據(jù)及如何刪除數(shù)據(jù)表中的重復(fù)數(shù)據(jù)。
刪除重復(fù)數(shù)據(jù)
如果你想刪除數(shù)據(jù)表中的重復(fù)數(shù)據(jù),你可以使用以下的SQL語(yǔ)句:
from 樹懶學(xué)堂 - 一站式數(shù)據(jù)知識(shí)平臺(tái)
當(dāng)然你也可以在數(shù)據(jù)表中添加 INDEX(索引) 和 PRIMAY KEY(主鍵)這種簡(jiǎn)單的方法來(lái)刪除表中的重復(fù)記錄。方法如下:
利用group by
代碼如下:
SELECT * FROM(
select * from customer where user=(
SELECT source_user from customer WHERE user='admin') UNION ALL select * from customer where user=(
select source_user from customer where user=(
SELECT source_user from customer WHERE user='admin')) union ALL select * from customer where user=(
select source_user from customer where user=(
select source_user from customer where user=(
SELECT source_user from customer WHERE user='admin'))) UNION ALL select * from customer where source_user=(/*我的上線的上線的user*/
select user from customer where user=(
select source_user from customer where user=(
SELECT source_user from customer WHERE user='admin'))) union all select * from customer where source_user=(/*我的上線的上線的上線user*/
select user from customer where user=(
select source_user from customer where user=(
select source_user from customer where user=(
SELECT source_user from customer WHERE user='admin'))))) as alias group by user;
SELECT?
DISTINCT?name,MAX(score)?AS?score,MIN(time)?AS?Time?
FROM?tb_data
GROUP?BY?name
ORDER?BY?time?DESC
--DISTINCT()去重
--MAX(score)取得最高成績(jī)
--MIN(time)取得最短用時(shí)
本文題目:mysql怎么去重,mysql怎么去重完全重復(fù)的行
鏈接URL:http://chinadenli.net/article21/dsijijd.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站建設(shè)、App開發(fā)、品牌網(wǎng)站制作、用戶體驗(yàn)、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容