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

為了賬號安全,請及時綁定郵箱和手機立即綁定

不會最后這個作業

老師,這個題目太難,不會啊。怎么把查詢結果刪除?

正在回答

3 回答

對樓上的回答簡化下REPLACE部分:

update user1 b join (

? select user_name,group_concat(

? ? distinct SUBSTRING_INDEX(SUBSTRING_INDEX(mobile,',',t.id),',',-1)

? ? ##直接取第t.id個mobile

? ) mobile from (

? ? select user_name,mobile,length(concat(mobile,','))-length(replace(mobile,',','')) size from user1

? ) a cross join tb_sequence t on a.size>=t.id group by a.user_name

) c on b.user_name = c.user_name set b.mobile = c.mobile;


1 回復 有任何疑惑可以回復我~

update user1 b join (select user_name,group_concat(distinct replace(substring(substring_index(mobile,',',b.id),length(substring_index(mobile,',',b.id-1))+1),',','')) mobile from (select user_name,mobile,length(concat(mobile,','))-length(replace(mobile,',','')) size from user1) a join tb_sequence b on a.size>=b.id group by a.user_name) c on b.user_name = c.user_name set b.mobile = c.mobile;只要看了老師講的行轉列,這道題就不會太難。其中c表就是老師講行轉列的稍加處理之后,在關聯更新就好了。

1 回復 有任何疑惑可以回復我~

##準備好要用的表

create table user1_practice(id int not null auto_increment primary key,

user_name varchar(3),

over varchar(5),

mobile varchar(100));

insert into user1_practice(user_name,over,mobile) values ('唐僧','旃檀功德佛','12112345678,14112345678,12112345678');

insert into user1_practice(user_name,over,mobile) values ('豬八戒','凈壇使者','12144643321,14144643321');

insert into user1_practice(user_name,over,mobile) values ('孫悟空','斗戰勝佛','12166666666,14166666666,18166666666,18166666666');

insert into user1_practice(user_name,over,mobile) values ('沙僧','金身羅漢','12198343214,14198343214');

##建一個序列表

create tb_sequence(id int not null auto_increment primary key);

insert into tb_sequence values(),(),(),(),(),(),(),(),();

##列轉行后的表user1_trans1

create table user1_trans1 as?

select a.id,user_name,over,

replace(substring(substring_index(mobile,',',a.id),char_length(substring_index(mobile,',',a.id-1))+1),',','') as mobile?

from tb_sequence a cross join(

select user_name,over,

concat(mobile,',') as mobile,

length(mobile)-length(replace(mobile,',',''))+1 as size

from user1_practice b) b on a.id <= b.size;

##刪除user1_trans1表中的重復記錄

delete a from user1_trans1 a join (

select user_name,over,mobile,count(*),max(id) as id?

from user1_trans1 ?group by user_name,over,mobile having count(*) > 1 ?) b

on a.user_name = b.user_name?

and a.over = b.over

and a.mobile = b.mobile

where a.id < b.id;

##用group_concat函數將mobile轉化成以逗號分隔的字符串

create table user1_trans2 as

select user_name,over,group_concat(mobile) as mobile from user1_trans1

group by user_name,over;

##對原表user1_practice進行關聯更新

update user1_practice a inner join user1_trans2 b on a.user_name = b.user_name

set a.mobile = b.mobile;?


6 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

不會最后這個作業

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

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

公眾號

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