MySQL嵌套的select语句问题

时间:2017-06-22 21:12:19

标签: mysql

我正在努力让我的嵌套SQL语句起作用。

它告诉我第二个SELECT附近有语法错误。

有人可以提供建议吗?

SELECT date, Expense_Date, cost1
RIGHT JOIN t ON dates.date = t.Expense_Date
FROM (
    SELECT Expense_Date, IFNULL(Sum(Total_Cost),0) as cost1
    FROM  Expenses
    RIGHT JOIN membership_userrecords ON Expenses.id = membership_userrecords.pkValue
    where membership_userrecords.memberID = 'kieran' AND membership_userrecords.tableName='Expenses'
    GROUP BY Expense_Date

) AS t
GROUP BY date ;

2 个答案:

答案 0 :(得分:1)

忘记第二个SELECT ...您在第一个FROM内缺少SELECT条款,如下所示

SELECT date, Expense_Date, cost1 <-- here
RIGHT JOIN t ON dates.date = t.Expense_Date

答案 1 :(得分:0)

试试这个

SELECT date, Expense_Date, cost1
FROM (
    SELECT Expense_Date, IFNULL(Sum(Total_Cost),0) as cost1
    FROM  Expenses
    RIGHT JOIN membership_userrecords ON Expenses.id = membership_userrecords.pkValue
    where membership_userrecords.memberID = 'kieran' AND membership_userrecords.tableName='Expenses'
    GROUP BY Expense_Date

) AS t
LEFT JOIN t ON dates.date = t.Expense_Date
GROUP BY date ;