mysql的insert into from select 和update by select
2011-05-20 17:09
471 查看
从contact表生成dialog表的uid, contact_uid, last_msg_id,
last_msg_time, total_msg_count:
insert into sns_user_00.user_dialog_00(uid,
contact_uid,last_msg_time, last_msg_id, totle_msg_count) select uid,
contact_uid, ctime as last_msg_time, msg_id as last_msg_id, count(*) as
total_msg_count from (select msg_id,
uid, contact_uid, ctime, type from sns_user_00.user_contact_00 order by ctime
desc) `temp` group by uid, contact_uid order
by ctime desc;
上面语句实现了insert into select,其中包括查询每一个类别中时间最晚的记录,利用嵌套select
从contact表生成dialog表的last_recv_time:
insert into
sns_user_00.user_dialog_00(uid, contact_uid, last_recv_time) select * from (
select uid, contact_uid, ctime as recv_time from sns_user_00.user_contact_00
where type=1 order by ctime desc ) `temp`
group by uid,contact_uid order by recv_time desc on duplicate key update last_recv_time =
values(last_recv_time);
上面语句实现了从结果集中去值然后update, 当然如果是多字段的话 last_recv_time = values(last_recv_time)可以写多个,以逗号隔开。
有时候可以利用mysql自身实现数据转移,不必从db导出为文本,然后再处理为新的插入语句,最后再执行一次导入。
last_msg_time, total_msg_count:
insert into sns_user_00.user_dialog_00(uid,
contact_uid,last_msg_time, last_msg_id, totle_msg_count) select uid,
contact_uid, ctime as last_msg_time, msg_id as last_msg_id, count(*) as
total_msg_count from (select msg_id,
uid, contact_uid, ctime, type from sns_user_00.user_contact_00 order by ctime
desc) `temp` group by uid, contact_uid order
by ctime desc;
上面语句实现了insert into select,其中包括查询每一个类别中时间最晚的记录,利用嵌套select
从contact表生成dialog表的last_recv_time:
insert into
sns_user_00.user_dialog_00(uid, contact_uid, last_recv_time) select * from (
select uid, contact_uid, ctime as recv_time from sns_user_00.user_contact_00
where type=1 order by ctime desc ) `temp`
group by uid,contact_uid order by recv_time desc on duplicate key update last_recv_time =
values(last_recv_time);
上面语句实现了从结果集中去值然后update, 当然如果是多字段的话 last_recv_time = values(last_recv_time)可以写多个,以逗号隔开。
有时候可以利用mysql自身实现数据转移,不必从db导出为文本,然后再处理为新的插入语句,最后再执行一次导入。
相关文章推荐
- Mysql的INSERT INTO SELECT与SELECT INTO FROM
- Insert Into ...Select 加 Update From 同步表数据
- mysql中insert into select from的使用
- mysql中复制表数据(select into from和insert into select)
- MySQL---insert into select from
- mysql中insert into select from的使用
- Insert Into ...Select 加 Update From 同步表数据
- mysql中insert into select from的使用
- 数据库_MySQL_mysql中insert into select from的使用
- MySQL中insert ignore into, on duplicate key update,replace into,insert … select … where not exist的一些用法总结
- mysql中insert into select from的使用
- mysql学习4:select,update,insert,delete
- SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE
- MySQL的Replaceinto与Insertinto.....onduplicatekeyupdate...真正的不同之处
- oracle insert into select * from tablename
- select into from 和 insert into select 的用法和区别
- mybatis插入oracle数据库,如果用foreash就用select xx from dual;否则用insert into tablename(,,)values()时不要用foreach
- 温故知新Insert into select & select into from
- mysql insert into ..select 发生死锁问题。
- MYSQL用法(十三) MySQL中INSERT INTO SELECT 的使用详解