列出未销售的客户和产品

k3fezbri  于 2021-07-24  发布在  Java
关注(0)|答案(3)|浏览(369)

需要使用union返回以下列(3):
所有没有发票的客户
所有未售出的产品
类别:这与“客户”或“产品”有关吗?打印“客户”或“产品”
id:customer.id(category=“customer”)或product.id(category=“product”)
名称:customer.customer\u name(category=“customer”)或product.product\u name(category=“product”)
table:
顾客
身份证件
客户名称
城市id
客户地址
联系人
电子邮件
电话
产品
身份证件
库存单位
产品名称
产品描述
当前价格
库存数量
发票
身份证件
发票号码
客户id
用户\帐户\ id
总价
发布时间
到期时间
支付的时间
取消的时间
退款时间
发票项目
身份证件
发票id
产品id

价格
行总价格
到目前为止,有以下几点:

SELECT 
  category,
  CASE
      WHEN category = 'customer' THEN c.id
      WHEN category = 'product' THEN p.id
  END AS 'id',
  CASE
    WHEN category = 'customer' THEN c.customer_name
    WHEN category = 'product' THEN p.product_name
  END AS 'name'
FROM 
  (
    SELECT
        CASE
          WHEN c.id = c.id THEN 'customer'
          WHEN p.id = p.id THEN 'product'
        END as 'category'
    FROM
        customer as c
    LEFT Join -- Left join to show all customers even those with & without invoices
        invoice as i
    ON c.id = i.customer_id
    AND i.id IS NULL -- Gives me all customers who do not have an invoice
    JOIN invoice_item as ii
    ON i.id = ii.invoice_id
    Join product p
    ON p.id = ii.product_id
  ) tb1

UNION ALL

SELECT 
  category,
  CASE
      WHEN category = 'customer' THEN c.id
      WHEN category = 'product' THEN p.id
  END AS 'id',
  CASE
    WHEN category = 'customer' THEN c.customer_name
    WHEN category = 'product' THEN p.product_name
  END AS 'name'
FROM
  (
    SELECT 
      CASE
        WHEN c.id = c.id THEN 'customer'
        WHEN p.id = p.id THEN 'product'
      END as 'category'
    FROM
        product as p
    LEFT JOIN -- Left join to show all products even those that sold and not sold
        invoice_item as ii
    ON p.id = ii.product_id
    AND ii.invoice_id IS NULL -- Gives me products that didnt sell
    JOIN invoice as i
    ON ii.invoice_id = i.id
  ) tb2

开放的任何建议,因为我一直在试图找出如何显示类别作为“产品”或“客户”。提前谢谢!

c9x0cxw0

c9x0cxw01#

考虑到您的数据模型和需求,您应该尝试下面的sql。你可以很容易地表演 UNION 使用两个sql。
first sql返回此列表-->所有没有发票的客户

select 'customer' as category, c.id as id, customer_name as name
     from customer c
     left join invoice i on c.id = i.customer_id
     where i.id is null

第二个sql返回这个列表-->所有未售出的产品

select 'product' as category, p.id as id, product_name as name
        from product p
        left join invoice_item ii on p.id = ii.product_id
        where ii.id is null;
ugmeyewa

ugmeyewa2#

嗯,它已经超过6个月了,但答案仍然是:

select 'customer' as category, c.id as id, customer_name as name
from customer c
left join invoice i on c.id = i.customer_id
where i.id is null 

union

select 'product' as category, p.id as id, product_name as name
from product p
left join invoice_item ii on p.id = ii.product_id
where ii.id is null;
r7s23pms

r7s23pms3#

SELECT 'customer' as category,id,customer_name FROM customer 
WHERE id NOT IN(SELECT customer_id FROM invoice) 
UNION 
SELECT 'product' as category,id,product_name FROM product 
WHERE id NOT IN(SELECT product_id FROM invoice_item);

相关问题