SQL用子查询结果更新多个字段,sql查询表字段
作者:iamlasong
要求:表格的内容需要变更,变更的内容放在一个临时表中,用SQL语句更新正式表中多个字段。
如果更新一个字段,直接用字段名=子查询就可以了,多个字段更新,将字段在括号中并列写出即可,如下:
update tb_jg t
set t.jgfl = 'sd',
( t.zj_code, t.zj_mc) = (select a.zj_code, a.zj_mc
from song_temp a
where a.zj_code = t.zj_code)
where exists (select 1 from song_temp a where a.zj_code = t.zj_code)
需求:根据子查询创建表,语句如下:
create table sncn_zd_jg as select * from tb_jg where zdbz='1' order by city_code,xs_code;
如果表已经存在,下面语句则可插入查询结果:
insert into sncn_zd_jg select * from tb_jg where zdbz='1' order by city_code,xs_code;
通用
select * from t1
where exists(select * from(select name,number,max(money) as money from t1 group by name,number) as tmp
where name=t1.name and number=t1.number
and money=t1.money)
上面报错:返回值
update dd
set dprice IN (select sum(book.price*orderdetail.number) from orderdetail,book,dd
where book.bookno=orderdetail.bookno and dd.ddno=orderdetail.ddno
group by dd.ddno)
from orderdetail,book
where orderdetail.bookno=book.bookno
————————————————————————————————————
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。