2011-11-18 64 views
2

我正在使用此第三方報告生成軟件。它有以下步驟:更改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 
+0

什麼是報告生成軟件?我想記下這一點,所以我可以告訴我的老闆避免它... – jwiscarson

+0

@jwiscarson eWebReports是軟件 – 0x4f3759df

+0

我想這是我的錯。所以他們的軟件不是太怪。 – 0x4f3759df

回答