求救,在sql结果集中追加一条或多条记录(100分)

  • 主题发起人 主题发起人 xnfzy
  • 开始时间 开始时间
X

xnfzy

Unregistered / Unconfirmed
GUEST, unregistred user!
select * from table1<br>where ..得到如下结果:<br>BA-2-CTN-1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>BA-2-WK-1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>BA-1-GBAN-2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>BA-2-DKA-4-16 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>BA-2-DKA-4-05 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>LBL-20-4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>E3A3121K1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>903W07033TG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>DM4857TG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>114E17830G &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>LBL-17-5.5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>在这个结果集中,DM4857TG 有替代的物料(BOM) DM4832T or DM1548TG, 这个替代物料来自table2,问题关键是如何把替代物料追加到主料的后一行?如果其它主料也有替代物料,又是如何追加到主料下一条?如下:<br>BA-2-CTN-1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>BA-2-WK-1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>BA-1-GBAN-2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>BA-2-DKA-4-16 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>BA-2-DKA-4-05 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>LBL-20-4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>E3A3121K1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>903W07033TG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>DM4857TG &nbsp; <br>&nbsp;DM4832T 替代 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>114E17830G <br>&nbsp;1548EGXXPG 替代 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>LBL-17-5.5
 
容易啊,你將查詢結果存到一個臨時表,然後將table2中的結果插入到臨時表,然後再顯示出來就可以了啊
 
忘了說了,你要將table1查出來的結果排一下序,然後,table2插入到臨時表中時,也按順序進行排,這樣就可以找到對應的地方了啊
 
是的,我也是用临时表,但是问题是,查询出来的结果,不能插入到指定的物料下一行!也就说,当主料有替代料时,那主料下一条记录显示必为替代料!
 
如何添加结果集,行值?也就是说,结果的第一行1,第二行2,....第100行,值是100?
 
用的什么数据库?用ORACLE就简单了,<br>求个合集,从合集中取记录进行排序,一个复合查询就可以了
 
说的不明白,数据库不会只有一个字段吧,至少应该有主键吧,这样在现在的时候在主键上做文章处理一下,字表的数据自动成为他的子项就可以了,如果还不会,直接联系我
 
SELECT &nbsp;代码 &nbsp;FROM<br>(<br>SELECT &nbsp;物料代码 , 物料代码 &nbsp;AS &nbsp;代码 &nbsp;FROM TABLE1<br>UNION ALL<br>SELECT &nbsp;物料代码+'Z' ,' &nbsp;'+ [替代物料代码] +' 替代' FROM TABLE2 WHERE EXISTS(<br>&nbsp; &nbsp;SELECT * FROM TABLE1 WHERE TABLE1. 物料代码 =TABLE2. 物料代码 )<br>)T ORDER BY &nbsp;物料代码 , 代码
 
用主从表吧
 
后退
顶部