线在等。马上给分。建库有文本也粘上来了。可以直接帮忙。
create table team
(
team_autoid int identity(1,1) primary key,
teamID varchar(50),/*编号*/
team_state varchar(50),/*状态*/
team_name varchar(50), /*姓名*/
team_people int,/*人数*/
)
INSERT team SELECT 'TEM111111111 ', '正常 ', '王工 ',3434
INSERT team SELECT 'TEM122222222 ', '正常 ', '李鹏 ',1212
INSERT team SELECT 'TEM133333333 ', '正常 ', '人地 ',1212
INSERT team SELECT 'TEM114444444 ', '不正常 ', '中要 ',20000
INSERT team SELECT 'TEM155555555 ', '不正常 ', '肖有 ',55555
INSERT team SELECT 'TEM1666666666 ', '正常 ', 'dd ',1666
create table reserve
(
reserve_autoid int identity(1,1) primary key,
reserve_people varchar(50),/*人数*/
reserve_id varchar(50), /*id号*/
reserve_name varchar(50),/*产口名称*/
)
INSERT reserve SELECT 5,1, '桔子 '
INSERT reserve SELECT 24,1, '桔子 '
INSERT reserve SELECT 5,1, '桔子 '
INSERT reserve SELECT 24,4, '苹果 '
INSERT reserve SELECT 24,4, '桔子 '
select* from team
select* from reserve
想要的结果为一表的数据照样列。加上一个字段sumPeople,这个字段是由第二个表里reserve_id相同的id号 reserve_people这个
字段之合添写到一表里相同的id号里。如果二表没有的添到一表里的为0
例如结果为:
1TEM111111111正常王工3434 34
2TEM122222222正常李鹏1212 0
3TEM133333333正常人地1212 0
4TEM114444444不正常中要20000 48
5TEM155555555不正常肖有55555 0
6TEM1666666666正常dd 1666 0
谢谢高手帮我,谢谢
[解决办法]
select *,
sumPeople=isnull((select sum(cast(reserve_people as int)) from reserve where reserve_id=tmp.team_autoid), 0)
from team as tmp