两表连接问题
table1如下
id score
1 78
2 89
3 85
table2:
id score
1 87
2 78
3 74
我现在想把表连接成如下的形式
id score
1 78
1 87
2 89
2 78
3 85
3 74
用什么sql语句怎么样才能作到呢?
问题点数:50、回复次数:11Top
1 楼wangfei2428(龙飞)回复于 2002-05-24 10:38:00 得分 0
是不是见两个表的数据联合组成一个表,还是有特殊的要求?Top
2 楼crodling(十方)回复于 2002-05-24 10:59:02 得分 0
由于有特殊的要求,就是把两个表的数据联合组成一个,他们的字段名相同。现在需要把它们联合成上面我说的那种形式Top
3 楼wangfei2428(龙飞)回复于 2002-05-24 11:11:23 得分 0
sql="insert into table1 (id,score) select id,score from table2 "
Top
4 楼wangfei2428(龙飞)回复于 2002-05-24 11:13:29 得分 5
上面错了,应该是
sql="insert table1 (id,score) select id,score from table2 "Top
5 楼wangfei2428(龙飞)回复于 2002-05-24 11:19:32 得分 0
上面的还是不对,您的id是自动编号的字段还是有关联?Top
6 楼crodling(十方)回复于 2002-05-24 17:39:40 得分 0
to 龙飞,我的id是主键,并非自动编号
恩,我的思路也许有点问题,我现在想得到的结果是使用一个select语句就得出两个表中每个人有几个score是大于80的
table1如下
id score
1 78
2 89
3 85
table2:
id score
1 87
2 78
3 74
连接成table3
id score
1 78
1 87
2 89
2 78
3 85
3 74
也就是想得到这样的结果,
select id,count(*) from table3 where score>=80 group by idTop
7 楼hhytsoft(雨中独行)回复于 2002-05-24 18:38:22 得分 0
建个临时表,行不行呀,另外建个主键吧,Top
8 楼superdullwolf(超级大笨狼,每天要自强,MVP)回复于 2002-05-24 19:25:43 得分 0
如果不是自动增长的很容易嘛,做个循环,一条一条的insert into不就行了吗?不要用id 做主键Top
9 楼tripofdream(梦之旅)回复于 2002-05-24 21:08:40 得分 10
使用union
(select * from table1) union (select * from table2) order by idTop
10 楼crodling(十方)回复于 2002-05-25 14:56:54 得分 0
to tripofdream(梦之旅)
已经连接成表三的结构,能不能看一下有什么办法可以的出这样的结果呢
table1如下
id score
1 78
2 89
3 85
table2:
id score
1 87
2 78
3 74
连接成table3
id score
1 78
1 87
2 89
2 78
3 85
3 74
也就是想得到这样的结果,每个id的优良科数有几门
select id,count(*) from table3 where score>=80 group by id
Top
11 楼qiushuiwuhen(秋水无恨)回复于 2002-05-25 15:27:54 得分 35
select id,count(*) from ((select * from table1) union (select * from table2)) a where a.score>=80 group by a.id
Top




