2016-08-05 1 views
-1

2テーブル:tb_order、tb_receipt。 3-4個の受注から1つの領収書。しかし、同じコードを持つ注文、名前のみを1つの領収書にまとめることができます。 だから、tb_receiptの大部分のフィールドとtb_orderのコードを選択するselect節を作成したいのですが、内部結合を使うと冗長な結果しか得られません。どうすれば対処できますか?私は唯一の参加と考えることができます1つの領収書への複数の注文、冗長な結果との選択句、どのように?

tb_order: receiptid, name, code, product, total 

tb_receipt: receiptid, sum 

は:

select a.sum, b.code, b.name from tb_reciept a inner join tb_order b on a.receiptid = b.receiptid 

しかし、それは私かもしれないが、いくつかのために要する1枚の領収書として冗長結果が得られます。

サンプルデータ:tb_order:

receiptid code name total 

201601  001 iphone5 100 

201601  001 iphone5 500 

201601  001 iphone5 300 

tb_receipt:

receiptid sum 

201601  900 

期待:

receiptid code name  sum 

201601  001 iphone5 900 
+1

あなたは何を試してみましたか?あなたのデータはどのように見えますか?すべての標準的な質問... – HoneyBadger

+0

サンプルデータと期待される結果を示してください。 –

答えて

0

はあなたが外部キーtb_order.receipt_idによってtb_receiptするtb_orderからの参照を持っていると仮定すると、あなたは使用することができます

select distinct tb_receipt.*, tb_order.code, tb_order.name 
from tb_receipt inner join tb_order on tb_order.receipt_id = tb_receipt.id 
where <...> 

以上の効果

select tb_receipt.*, o.code, o.name 
from tb_receipt inner join (
    select distinct code, name, receipt_id from tb_order where <...> 
) as o on o.receipt_id = tb_receipt.id 
関連する問題