改变MySQL查询关键字的顺序?

时间:2011-11-18 17:23:38

标签: mysql

我正在使用此第三方报告生成软件。它有以下步骤:

  1) insert your SQL statement into a webpage.  
  2) invoke an API to send the a set of primary keys to the Query
  3) A Report is generated.

不幸的是,该软件很愚蠢,只需在SQL语句后添加WHERE子句即可。但是对于MySQL,WHERE语句应该在GROUP BY之前。因此,当API附加WHERE时,它会失败,因为它的SQL无效。有没有办法让MySQL在最后期望WHERE语句?

select incident.incidentID,
GROUP_CONCAT(moccode2.Description) as MOC2Description 
from incident
join incidentmoc on incident.IncidentID = incidentmoc.IncidentID
inner join moccode2 on moccode2.id = incidentmoc.moccodeid
/* WHERE should go here */ 
group by incident.incidentID
/* I want the WHERE to go here */ 

Derek Kromm在我要求的基本上是正确的,不幸的是我有额外的限制。它仍然会附加WHERE。

所以我尝试了这个:

 select incident.incidentID,
    GROUP_CONCAT(moccode2.Description) as MOC2Description 
    from incident
    join incidentmoc on incident.IncidentID = incidentmoc.IncidentID
    inner join moccode2 on moccode2.id = incidentmoc.moccodeid
    group by incident.incidentID
    HAVING incident.IncidentID > 1
   ////////////////////////////////////////
    now software appends WHERE invalid SQL

1 个答案:

答案 0 :(得分:1)

使用HAVING关键字

此链接包含有关使用它的一些详细信息:http://www.mysqltutorial.org/mysql-having.aspx