0

我有两个表,一个 transactions 表和 transactions_items 表:

交易:

ID Customer Amt
1  Bill     500
2  Helen    100

交易项目:

ID Code Price
1  R2   100
1  R3   200
1  R5   100
2  R5   100

我想将它们分组:

ID Customer Code Price Amt
1  Bill     R2   100   500
            R3   200   
            R5   100
2  Helen    R5   100   100

这可能吗?

4

2 回答 2

0

你可以这样做,但你有很多工作要做,在你的应用程序级别做,你将需要这个查询,

SELECT  a.*, 
        b.code,
        b.price,
        c.amt
FROM    transaction a
        INNER JOIN transaction_items b
            ON a.ID = b.ID
        INNER JOIN
        (
            SELECT ID, SUM(Price) Amt
            FROM transactions_Items
            GROUP BY ID
        ) c ON a.ID = c.ID
ORDER BY b.code
于 2012-10-11T06:15:55.207 回答
0

你可以通过这个

select transactions.id,transactions.customer,transaction_items.code,transaction_items.price,transactions.amount 
from transactions  
left outer join 
transaction_items  on a.id=b.id;
于 2012-10-11T06:21:11.640 回答