mysql 所有客户和产品的销售数据

k97glaaz  于 2023-01-25  发布在  Mysql
关注(0)|答案(1)|浏览(136)

查询应返回所有客户,甚至是没有发票的客户,还应返回产品,甚至是未售出的产品。对于空客户、空产品或空数量,打印NA。按customerid、productid和invoiceid排序
表格:
客户

id
customer_name
city_id
customer_address
contact_person
email
phone

产品

id
sku
product_name
product_description
current_price
quantity_in_stock

发票

id
invoice_number
customer_id
user_account_id
total_price
time_issued
time_due
time_paid
time_canceled
time_refunded

发票_物料

id
invoice_id
product_id
quantity
price
line_total_price

我试过这个

select 
   coalesce((c.customer_name), "NA"),
   coalesce((p.product_name),"NA"),
   coalesce((ii.quantity),"NA")
from customer c 
left join invoice i on c.id=i.customer_id
join invoice_item ii on  ii.invoice_id=i.id
left join product p on p.id=ii.product_id
order by i.customer_id,ii.product_id,ii.id
laik7k3q

laik7k3q1#

SELECT a.客户_名称,a.产品_名称,a.数量FROM(SELECTcustomer.idAS客户_ID,product.idAS产品_ID,发票_项目.ID AS发票_项目_ID,IFNULL(名称,“N/A”)客户_名称,IFNULL(Product Name,“N/A”)产品_名称,IFNULL(数量,0)数量FROM((www.example.com上的客户左联接发票customer.id=发票.客户_ID)左联接发票_发票项目上的项目.发票ID =invoice.id)左联接发票项目上的产品.产品ID =product.idUNION ALL SELECTcustomer.idproduct.id,发票项目.ID,IFNULL(名称,“不适用”)客户名称,IFNULL(Product Name,“不适用”)产品名称,如果为空(数量,0)数量自((产品左联接发票项目在发票项目上.产品标识=产品.标识)左联接发票在发票项目上.发票标识=www.example.com )在www.example.com上的左联接客户=customer.id发票。客户_id)订单依据客户_id、产品_id、发票_item_id;

相关问题