gpt4 book ai didi

mysql - UNION MySQL 的问题

转载 作者:行者123 更新时间:2023-11-30 23:58:55 25 4
gpt4 key购买 nike

我有这个问题:

SELECT COUNT(*) AS invoice_count, IFNULL(SUM(qa_invoices.invoice_total), 0)
AS invoice_total, IFNULL(SUM(qa_invoices.invoice_discount) ,0) AS invoice_discount
FROM qa_invoices
WHERE (DATE(qa_invoices.invoice_date) BETWEEN '12/06/25' AND '12/06/25')
AND qa_invoices.status_code IN (5, 8)

UNION

SELECT IFNULL(SUM(qa_returns.client_credit), 0)
FROM qa_returns
WHERE (DATE(qa_returns.returnlog_date) BETWEEN '12/06/25' AND '12/06/25');

我得到错误:

The used SELECT statements have a different number of columns.

我正在尝试使用 UNION 命令加入这 2 个选择,如果我们查看 returnlog_dateinvoice_date 具有相同的数据条件,如果有任何方法可以执行将两个查询合二为一会更好。

最佳答案

使用子选择:

SELECT
COUNT(*) AS invoice_count,
IFNULL(SUM(invoice_total), 0) AS invoice_total,
IFNULL(SUM(invoice_discount), 0) AS invoice_discount,
(
SELECT IFNULL(SUM(qa_returns.client_credit), 0)
FROM qa_returns
WHERE qa_returns.returnlog_date >= '2012-06-25'
AND qa_returns.returnlog_date < '2012-06-26'
) AS client_credit
FROM qa_invoices
WHERE invoice_date >= '2012-06-25'
AND invoice_date < '2012-06-26'
AND status_code IN (5, 8)

关于mysql - UNION MySQL 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11215110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com