查询Mysql有什么问题?

时间:2019-07-01 21:21:48

标签: mysql

有一个简单的SQL查询:

SELECT enterprise_invoces.AC_name, 
enterprise_invoces.AC_id, 
enterprise_invoces.AC_code, 
ABS(SUM(IF(AT_amount>0, AT_amount, 0)) AS debit,
ABS(SUM(IF(AT_amount <0, AT_amount, 0)) AS credit 
    FROM account_transactions 
    INNER JOIN enterprise_invoces ON enterprise_invoces.AC_id = account_transactions.AT_code

我收到此错误消息:

> # 1064 - You have an error in the request. Check the MySQL version documentation for correct syntax near 'FROM account_transactions INNER
> JOIN enterprise_invoces ON enterprise_invoces.AC' on line 1

1 个答案:

答案 0 :(得分:4)

您需要将方括号括起来

SELECT enterprise_invoces.AC_name, 
  enterprise_invoces.AC_id, 
  enterprise_invoces.AC_code, 
  ABS(SUM(IF(AT_amount>0, AT_amount, 0))) AS debit,  --here
  ABS(SUM(IF(AT_amount <0, AT_amount, 0))) AS credit  -- here
FROM account_transactions 
JOIN enterprise_invoces 
  ON enterprise_invoces.AC_id = account_transactions.AT_code
 GROUP BY enterprise_invoces.AC_name,
          enterprise_invoces.AC_id,
          enterprise_invoces.AC_code   -- there should be corresponding `GROUP BY`