这样的sql语句应该怎样写
create table a
(
id int,
je1 money,
je2 money,
je3 money,
je4 money,
je5 money,
je6 money,
je7 money,
primary key(id)
)
create table b
(
id int,
aid int,
iType varchar(10),
je money,
primary key(id)
)
--
insert a select 1,0,0,0,0,0,0,0
union select 2,0,0,0,0,0,0,0
union select 3,0,0,0,0,0,0,0
union select 4,0,0,0,0,0,0,0
union select 5,0,0,0,0,0,0,0
union select 6,0,0,0,0,0,0,0
union select 7,0,0,0,0,0,0,0
insert b select 1,1,'type1',10
union select 2,1,'type2',10.5
union select 3,1,'type2',1
union select 4,2,'type1',2
union select 5,1,'type5',12
union select 6,3,'type1',13
union select 7,1,'type2',14
union select 8,2,'type3',15
union select 9,3,'type5',16
union select 10,4,'type6',17
union select 11,5,'type7',18
union select 12,5,'type2',19
union select 13,1,'type3',19
union select 14,7,'type4',10
union select 15,1,'type5',11
union select 16,6,'type6',111
现在想写一条更新表a的语句,要求把表b里的je合计按照表a的id和表b的aid对应,按表b的类型不同填到表a的不同字段里
例如表b的type1就填到表a的je1里
想了半天也没有想出来简单的
update a
set je1=(select sum(je) from b where a.id=b.aid and b.iType='type1')
,je2=(select sum(je) from b where a.id=b.aid and b.iType='type2')
,je3=(select sum(je) from b where a.id=b.aid and b.iType='type3')
,je4=(select sum(je) from b where a.id=b.aid and b.iType='type4')
,je5=(select sum(je) from b where a.id=b.aid and b.iType='type5')
,je6=(select sum(je) from b where a.id=b.aid and b.iType='type6')
,je7=(select sum(je) from b where a.id=b.aid and b.iType='type7')
select * from a
感觉这么写太笨了,请高手看看
(
id int,
je1 money,
je2 money,
je3 money,
je4 money,
je5 money,
je6 money,
je7 money,
primary key(id)
)
create table b
(
id int,
aid int,
iType varchar(10),
je money,
primary key(id)
)
--
insert a select 1,0,0,0,0,0,0,0
union select 2,0,0,0,0,0,0,0
union select 3,0,0,0,0,0,0,0
union select 4,0,0,0,0,0,0,0
union select 5,0,0,0,0,0,0,0
union select 6,0,0,0,0,0,0,0
union select 7,0,0,0,0,0,0,0
insert b select 1,1,'type1',10
union select 2,1,'type2',10.5
union select 3,1,'type2',1
union select 4,2,'type1',2
union select 5,1,'type5',12
union select 6,3,'type1',13
union select 7,1,'type2',14
union select 8,2,'type3',15
union select 9,3,'type5',16
union select 10,4,'type6',17
union select 11,5,'type7',18
union select 12,5,'type2',19
union select 13,1,'type3',19
union select 14,7,'type4',10
union select 15,1,'type5',11
union select 16,6,'type6',111
现在想写一条更新表a的语句,要求把表b里的je合计按照表a的id和表b的aid对应,按表b的类型不同填到表a的不同字段里
例如表b的type1就填到表a的je1里
想了半天也没有想出来简单的
update a
set je1=(select sum(je) from b where a.id=b.aid and b.iType='type1')
,je2=(select sum(je) from b where a.id=b.aid and b.iType='type2')
,je3=(select sum(je) from b where a.id=b.aid and b.iType='type3')
,je4=(select sum(je) from b where a.id=b.aid and b.iType='type4')
,je5=(select sum(je) from b where a.id=b.aid and b.iType='type5')
,je6=(select sum(je) from b where a.id=b.aid and b.iType='type6')
,je7=(select sum(je) from b where a.id=b.aid and b.iType='type7')
select * from a
感觉这么写太笨了,请高手看看
...全文
相关推荐
关于在Java代码中写Sql语句需要注意的问题
近做程序,时不时需要自己去手动将sql语句直接写入到Java代码中,写入sql语句时,需要注意几个小问题。 先看我之前写的几句简单的sql语句,自以为没有问题,但是编译直接报错。 String str = insert into XXX(a,b,c) values ('a.getA()','a.getB()','a.getC()');; 研究了半天发现应该是连接字符串问题,第一次修改过后将赋值字段前后加“+”号来完成sql语句。改正后代码如下 String str = insert into XXX(a,b,c) values ('+a.getA()
通过Navicat 写SQL语句
具体步骤: 1、首先打开要写SQL语句的数据库连接 2、然后点击菜单中的查询(query) 3、然后点击新建查询 4、然后输入SQL语句,点击运行,查询到的数据在底部
学会idea列操作,快速写SQL语句
idea列操作,指的是我们可以同时操作多列。在我们写sql语句的插入和更新等需要输入属性名时,可以以我们创建的类为基础,通过idea的多列操作,我们可以简单方便的获取到属性名,而不用自己去复制或者手动敲。
C#不写SQL语句的数据库操作
不写SQL语句的数据库操作,实现了对数据的增删改查功能!
如何写高效sql语句
NULL 博文链接:https://uusoft.iteye.com/blog/1195344


疑难问题
