我试过了这个查询
SELECT invoices.customer_id AS customer_id, invoices.id AS id, invoices.date AS date, due_date, bonus.date AS p_date, invoices.paid_date, total, bonus.amount AS p, invoices.paid, invoices.balance
FROM (
(
SELECT inv.customer_id AS customer_id, inv.id AS id, inv.order_id AS order_id, inv.date AS date, due_date,
GROUP_CONCAT( CONCAT( DATE_FORMAT( p.date, '%d/%m/%Y' ) , ' (', p.amount, ')' ) SEPARATOR ', ' ) AS paid_date,
total, COALESCE( sum( p.amount ) , 0 ) AS paid,
( total - COALESCE( sum( p.amount ) , 0 ) ) AS balance
FROM (
invoices inv
)
LEFT JOIN payments p ON p.invoice_id = inv.id
GROUP BY inv.id
)invoices
)
FULL OUTER JOIN bonus ON bonus.customer_id = invoices.customer_id
WHERE invoices.customer_id = '4'
GROUP BY invoices.order_id
ORDER BY date DESC
LIMIT 10
并且得到SQL语法错误但是如果我尝试LEFT / RIGHT而不是FULL OUTER JOIN并且使用单个roe结果。
CUSTOMER_ID ID DATE DUE_DATE P_DATE PAID_DATE TOTAL P PAID BALANCE
4 1 April, 24 2014 09:38:26+0000 April, 30 2014 00:00:00+0000 May, 10 2014 05:02:31+0000 28/04/2014 (10.00), 25/04/2014 (10.00), 25/04/2014 (150.00), 25/04/2014 (390.00) 680 100 560 120
现在问题是我需要P_DATE和P作为单独的行来显示奖金。你能帮我修改我的查询,这样我就可以获得奖金表记录P_DATE和P作为单独的行,其他作为单独的行。
谢谢
答案 0 :(得分:1)
试试这个,
SELECT invoices.customer_id AS customer_id, invoices.id AS
id, invoices.date AS DATE, due_date AS due_date, invoices.paid_date AS paid_date, total AS total, invoices.paid AS paid, invoices.balance AS balance
FROM (
(SELECT inv.customer_id AS customer_id, inv.id AS id, inv.order_id AS order_id, inv.date AS DATE, due_date,
GROUP_CONCAT( CONCAT( DATE_FORMAT( p.date, '%d/%m/%Y' ) , ' (', p.amount, ')' ) SEPARATOR ', ' ) AS paid_date,
total, COALESCE( SUM( p.amount ) , 0 ) AS paid,
( total - COALESCE( SUM( p.amount ) , 0 ) ) AS balance
FROM (
invoices inv
)
RIGHT JOIN payments p ON p.invoice_id = inv.id
GROUP BY inv.id
)invoices
)
RIGHT JOIN bonus ON bonus.customer_id = invoices.customer_id
WHERE invoices.customer_id = '4'
GROUP BY invoices.order_id
UNION
SELECT '' AS customer_id, '' AS
id, '' AS DATE, '' due_date, bonus.date AS paid_date, '' total, bonus.amount AS paid, '' balance
FROM (
(SELECT inv.customer_id AS customer_id, inv.id AS id, inv.order_id AS order_id, inv.date AS DATE, due_date,
GROUP_CONCAT( CONCAT( DATE_FORMAT( p.date, '%d/%m/%Y' ) , ' (', p.amount, ')' ) SEPARATOR ', ' ) AS paid_date,
total, COALESCE( SUM( p.amount ) , 0 ) AS paid,
( total - COALESCE( SUM( p.amount ) , 0 ) ) AS balance
FROM (
invoices inv
)
RIGHT JOIN payments p ON p.invoice_id = inv.id
GROUP BY inv.id
)invoices
)
RIGHT JOIN bonus ON bonus.customer_id = invoices.customer_id
WHERE invoices.customer_id = '4'
GROUP BY invoices.order_id
ORDER BY DATE DESC
LIMIT 10
答案 1 :(得分:0)
我认为不可能像你说的那样(你应该使用两个SQL语句) 你能解释一下这个目标是什么,也许我们可以用其他方式来做。