0
find_by_sql("SELECT s.productcode, s.description, CAST(SUM(l.amount) as UNSIGNED) AS amount FROM softwares s LEFT JOIN licenses l ON s.id=l.software_id GROUP BY s.productcode, s.description WHERE s.supplier= 'softcat'")

我目前有这个,我得到了错误ActiveRecord::StatementInvalid in SoftwaresController#export_softcat

我试图在最后添加一个 where 子句,只选择某个供应商的记录。

4

3 回答 3

3
SELECT s.productcode, s.description, CAST(SUM(l.amount) as UNSIGNED) AS amount
FROM softwares s
    LEFT JOIN licenses l ON s.id=l.software_id
WHERE s.supplier = 'softcat'
GROUP BY s.productcode, s.description

试试这个方法

于 2012-08-22T11:11:26.800 回答
2

您需要在WHERE子句之前添加GROUP BY子句,从而有效地将 SQL 更改为:

SELECT s.productcode, s.description, CAST(SUM(l.amount) as UNSIGNED) AS amount
FROM softwares s 
LEFT JOIN licenses l ON s.id=l.software_id
WHERE s.supplier= 'softcat'
GROUP BY s.productcode, s.description
于 2012-08-22T11:11:10.247 回答
2

您必须将 GROUP BY 语句放在 WHERE 子句语句之后

于 2012-08-22T11:12:21.373 回答