group by 能否拼接两条查询记录?

来源:百度知道 编辑:UC知道 时间:2024/06/25 07:17:50
select Prod_CL as Prod_ID,b.Prod_Name as Prod_Name,b.Prod_Price as Price,Coll_Quantity as Quantity,(b.Prod_Price*Coll_Quantity) as Total
from tb_Order_Collocate a,tb_Product b
where Orde_ID = 10000 and a.Prod_CL = b.Prod_ID and Prod_CP in (select a.Prod_ID from tb_OrdersInfo a,tb_Product b where Orde_ID = 10000 and Ty_ID = 104 and a.Prod_ID = b.Prod_ID)
union all
select a.Prod_ID as Prod_ID,a.Prod_Name as Prod_Name,OrIn_Price as Price,OrIn_Quantity as Quantity,Orde_Total as Total
from tb_OrdersInfo a,tb_Product b
where Orde_ID = 10000 and Ty_ID = 102 and a.Prod_ID = b.Prod_ID
非常感谢 呵呵 !接分!

应该这样说group by 可以对并接(前后)的查询进行操作,但不能说group by 可以并接,并接还是要用union all才行。

使用办法:
1、并接前用group by ,这种简单,直接把两个带有分组group by 查询的语句用union all并接就可以了;

2、并接后用group by,那就要把整个并接后的数据集当视图操作,如:select a,b,sum(c) from
(
select a,b,c from table1
union all
select a,b,c from table2
)table_temp group by a,b

明白了吗?

可以

union 已经拼接起来了啊,你给的这个sql已经拼接了

把你上面的查询结果插入一个临时表,或者弄成一视图,
然后再用GROUP BY 对这个临时表或视图聚合分组就可以了!