2012-11-26 103 views
3

我習慣在Oracle數據庫上運行,所以我不太確定如何解決這個問題。我已經收窄我的查詢的一個簡單的例子如下:MySQL錯誤1064(v 5.0.96)GROUP BY子句

SELECT 0 as gm_rowID, 
'-ALL Grantmakers-' as grantmakerName 
FROM dual 
GROUP BY 2 

phpMyAdmin的運行,出現以下錯誤的SQL:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY 2 LIMIT 0, 30' at line 1 

Oracle可以運行此查詢就好了。 MySQL可以在沒有GROUP BY子句的情況下運行查詢。有任何想法嗎?

--Here是整個查詢:

SELECT 
    p.grantmaker_rowid as gm_rowID, 
    gm.grantmaker_companyName as grantmakerName 
FROM grantmaker_info gm, proposal_submission p 
WHERE 0=0 
AND p.grantmaker_rowid = gm.grantmaker_rowid 
UNION 
SELECT 
    0 as gm_rowID, 
    '-ALL Grantmakers-' as grantmakerName 
FROM dual 
ORDER BY 2 
GROUP BY 2 
LIMIT 0 , 30 
+0

請顯示整個查詢。 –

+0

@Chester:仍然不可能是整個查詢,因爲(至少)它缺少'GROUP BY'和'LIMIT'子句。 – eggyal

回答

2

Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column names, column aliases, or column positions. Column positions are integers and begin with 1

來源:http://dev.mysql.com/doc/refman/5.0/en/select.html

除非你只需要在該表1列,它應該運行正常。然而,我的建議是引用任何你想要的GROUP BY的列名(或別名)。

編輯:我唯一的其他建議是包括該表的SHOW CREATE TABLE輸出。

edit2:好的,我看你已經更新了你的問題。爲什麼不是ORDER BY 2,你ORDER BY grantmakerName(如果這是你想要命令的列?)

+0

「SHOW CREATE TABLE」是指表的列名嗎?查詢本身只是一個基於'DUAL'表的字面查詢。那應該不重要,對嗎? – Chester

+0

基本上爲查詢中引用的每個表運行'SHOW CREATE TABLE',例如:grantmaker_info,proposal_submission和dual。除了提供有關您的問題的其他詳細信息外,它還允許我們使用SQL Fiddle來幫助解決方案(http://sqlfiddle.com/)。 – ajacian81

+1

這裏是'SHOW CREATE TABLE grantmaker_info'的文件[https://dl.dropbox.com/u/6212377/grantmaker_info.sql](https://dl.dropbox.com/u/6212377/grantmaker_info.sql)和'SHOW CREATE TABLE proposal_submission' [https://dl.dropbox.com/u/6212377/proposal_submission.sql](https://dl.dropbox.com/u/6212377/proposal_submission.sql)。 Dual是一個系統表。 – Chester