如何使用union和join优化查询?是否有可能在联盟之外移动一些连接?
(
SELECT accounting.id, accounting.time, accounting.subaccount_id AS module_id, accounting.balance_invoice_id AS balance_id, accounting.currency_amount*-1 AS currency_amount, accounting.amount*-1 AS amount, enclosure.enc_id_, enclosure.txt, currency.name AS currency_name, IF(balance_invoice.amount, accounting.currency_amount*-1+SUM(balance_invoice.amount), accounting.currency_amount*-1) AS balance
FROM accounting
INNER JOIN enclosure ON enclosure.id=accounting.enc_id
LEFT JOIN currency ON currency.id=accounting.currency_id
LEFT JOIN balance_invoice ON balance_invoice.accounting_id=accounting.id
WHERE accounting.att_booked=1
GROUP BY accounting.id
)
UNION (
SELECT accounting.id, accounting.time, accounting.subaccountoff_id AS module_id, accounting.balanceoff_invoice_id AS balance_id, accounting.currency_amount, accounting.amount, enclosure.enc_id_, enclosure.txt, currency.name AS currency_name, IF(balance_invoice.amountoff, accounting.currency_amount+SUM(balance_invoice.amountoff), accounting.currency_amount) AS balance
FROM accounting
INNER JOIN enclosure ON enclosure.id=accounting.enc_id
LEFT JOIN currency ON currency.id=accounting.currency_id
LEFT JOIN balance_invoice ON balance_invoice.accounting_id=accounting.id
WHERE accounting.att_booked=1 && accounting.type IN (0,1,2)
GROUP BY accounting.id
) ORDER BY time DESC
答案 0 :(得分:0)
这也应该在没有UNION的情况下工作。我使用了几个CASE语句。未测试的:
SELECT a.id, a.time
,CASE WHEN a.type IN (0,1,2) THEN a.subaccountoff_id ELSE a.subaccount_id END AS module_id
,CASE WHEN a.type IN (0,1,2) THEN a.balanceoff_invoice_id ELSE a.balance_invoice_id END AS balance_id
,CASE WHEN a.type IN (0,1,2) THEN a.currency_amount ELSE a.currency_amount*-1 END AS currency_amount
,CASE WHEN a.type IN (0,1,2) THEN a.currency_amount ELSE a.amount*-1 END AS amount
,e.enc_id_, e.txt, c.name AS currency_name
,CASE WHEN a.type IN (0,1,2)
THEN IF (b.amountoff, a.currency_amount + SUM(b.amountoff), a.currency_amount)
ELSE IF (b.amount, a.currency_amount*-1 + SUM(b.amount), a.currency_amount*-1) END AS balance
FROM accounting a
JOIN enclosure e ON e.id = a.enc_id
LEFT JOIN currency c ON c.id = a.currency_id
LEFT JOIN balance_invoice b ON b.accounting_id = a.id
WHERE a.att_booked = 1
GROUP BY a.id
ORDER BY a.time DESC