亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么union前后得出的數據并不去除重復的?很多數據重復顯示兩次,求指教!

為什么union前后得出的數據并不去除重復的?很多數據重復顯示兩次,求指教!

躍然一笑 2022-05-11 13:09:16
我用union疊加的結果有重復一樣的數據(就是同一數據顯示兩次)暈,搜了一下,union默認就是去除重復的,而union ALL 才是全部顯示。不知道我的怎么回事,用的是Asp+Access代碼如下:暈啊,distinct我試了,還是重復,所以問題應該不在這。我想用1,2來排序,toadboy的方法實在是有點復雜啊。我是ASP寫SQL語句不好寫sql="select * from (select 1 as b, data.* from data WHERE name Like '哈哈' union select 2 as b, data.* from data where name Like '%"& 變量 &"%' or tag Like '哈哈') order by b asc"我是想前后兩個結果合并,優先顯示name Like '哈哈'的,這點做到了。
查看完整描述

4 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

看不到你寫的代碼,只好猜測一下:
sql="select * from (select 1 as b, data.* from data WHERE name Like '哈哈' union select 2 as b, data.* from data where name Like '%"& 變量 &"%' or tag Like '哈哈') order by b asc"

改成

sql="
select * from
(select 1 as b, data.*
from data
WHERE
name Like '哈哈'
and name not Like '%"& 變量 &"%'
and tag not Like '哈哈'
union
select 2 as b, data.*
from data
where
name not like'哈哈'
and (name Like '%"& 變量 &"%' or tag Like '哈哈'))
order by b asc"

想法就是:兩個select語句自己選滿足自己條件的記錄,同時,去除另一個select語句選出的記錄



查看完整回答
反對 回復 2022-05-16
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

你的結果集第一個前面加了個一 第二個前面加了個2 那樣肯定不重復了
比如
從第一個查詢返回
1 a b c
1 b c d
從第二個查詢返回
2 a b c
2 b c d

順便問一下你要前面的1,2有什么用 去掉不就好了嗎?


查看完整回答
反對 回復 2022-05-16
?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

drop table test1;
drop table test2;
create table test1 (a char(4),c char(4));
create table test2 (a char(4),c char(4));

insert into test1 values('aaa','bbb')
insert into test1 values('aaa','aaa')
insert into test2 values('ccc','aaa')
insert into test2 values('aaa','aaa')

select 1 as b,* from test1 where a like 'aaa'
union select 2 as b,* from test2 where c like 'aaa'
order by b
這個就是你那條語句,返回結果為
1 aaa aaa
1 aaa bbb
2 aaa aaa
2 ccc aaa

先在去除重復的范圍內選取,舉例如下:
select 1 as b,* from test1 where a like 'aaa'
union select 2 as b,* from test2 where c like 'aaa' and (a+c) not in (select a+c from test1)
order by b
返回結果:
1 aaa aaa
1 aaa bbb
2 ccc aaa

具體怎么改你的語句,根據實際情況來。我這個例子簡單,就是用(a+c) not in (select a+c from test1)說明一下去除重復的范圍。

 


查看完整回答
反對 回復 2022-05-16
?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

sql="select distinct * from (select 1 as b, data.* from data WHERE name Like '哈哈') union (select 2 as b, data.* from data where name Like '%"& 變量 &"%' or tag Like '哈哈') order by b asc"

查看完整回答
反對 回復 2022-05-16
  • 4 回答
  • 0 關注
  • 721 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號