简体   繁体   中英

SQL - inner join sum product

I have 2 tables "invoice" and "invoice_product".

In the first table I have columns like (id, invoice_date, buyer_name, total_price ...) I the second table I have products connected with buyer invoice (id, invoice_id, product_id, product_price, product_unit, amout). The same product can have different units (for example orange - sell bags and weight) Now I want to display something likie this

Input data ivnoive_product:

Product | unit | price | buyer  | invoice_id
Orange  | 1kg  | 5.50$ | Alfred | 55
Orange  | 2bags| 25$   | Alfred | 79
Orange  | 1kg  | 5.50$ | Sam    | 16
Banana  | 2kg  | 2.50$ | Alfred | 55 

a) sum client price Result display table with client sum:

Buyer | Sum price  | 
Alfred| 33$        | (include orange and banana)
Sam   | 5.50$      | 

a2) How can I select if i have where (with id_product == orange product) ...where id_product = 5

Buyer  | Sum price
Alfred | 30.5$ (without banana price)

b) sum product price Result display:

Product | Units      | Sum price
Orange  | 2kg, 2bags | 36.00$
Banana  | 2kg        | 2.50$

I don't want that my select sql query duplicate products and buyers like this:

Orange | 2kg | 11$
Orange | 2bags | 25$
Banana | 2kg | 2.5$

I want to use pagination (Zend framework) where in the first page it shows only ten first row returned by query. (If I have duplicate products, then I have less row in the first page)

Sum client price:

select buyer, sum(price ) 
from invoice_product
group by buyer  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM