在mysql中创建Where子句

时间:2012-08-22 11:08:48

标签: mysql ruby-on-rails activerecord where-clause

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子句,只选择某个供应商的记录。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

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

以这种方式试试

答案 2 :(得分:1)

您必须在WHERE子句声明

之后放置GROUP BY语句