2012-08-06 114 views
0

我有問題,這樣的代碼:SQL語法和SQL Server版本錯誤

$comments->query = "SELECT " . PREFIX . "_comments.id, post_id, " . PREFIX . "_comments.user_id, " . PREFIX . "_comments.date, " . PREFIX . "_comments.autor as gast_name, " . PREFIX . "_comments.email as gast_email, text, ip, is_register, name, " . USERPREFIX . "_users.email, news_num, " . USERPREFIX . "_users.comm_num, user_group, lastdate, reg_date, signature, foto, fullname, land, yahoo, " . USERPREFIX . "_users.xfields, " . PREFIX . "_post.title, " . PREFIX . "_post.date as newsdate, " . PREFIX . "_post.alt_name, " . PREFIX . "_post.category FROM " . PREFIX . "_comments LEFT JOIN " . PREFIX . "_post ON " . PREFIX . "_comments.post_id=" . PREFIX . "_post.id LEFT JOIN " . USERPREFIX . "_users ON " . PREFIX . "_comments.user_id=" . USERPREFIX . "_users.user_id " . $where . " ORDER BY id desc"; 

錯誤:

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 'ON dle_comments.post_id=dle_post.id LEFT JOIN dle_users ON dle_comments.user_id=' at line 1 

編輯:

SELECT 
    dle_comments.id, post_id, 
    dle_comments.user_id, 
    dle_comments.date, 
    dle_comments.autor as gast_name, 
    dle_comments.email as gast_email, 
    text, ip, is_register, 
    group_concat(mid) as `awards`, 
    name, dle_users.email, news_num, 
    dle_users.comm_num, user_group, 
    lastdate, reg_date, signature, 
    foto, fullname, land, icq, 
    dle_users.xfields, dle_post.title, 
    dle_post.date as newsdate, dle_post.alt_name, dle_post.category 
FROM 
    dle_comments 
    LEFT JOIN dle_awards 
    ON uid = dle_post 
    ON dle_comments.post_id=dle_post.id 
    LEFT JOIN dle_users 
    ON dle_comments.user_id=dle_users.user_id 
ORDER BY id desc 
LIMIT 0,30 

我的SQL版本:5.5 .20

我該如何解決這個問題?

+1

你可以發表迴應或打印的查詢?與所有PREFIX等,它幾乎是不可讀的... – arnoudhgz 2012-08-06 20:54:52

+0

我添加了一個完整的錯誤 – Alireza 2012-08-06 20:58:53

+0

請在您的查詢中放一些換行符....我試圖編輯它,但我沒有看到我的更改。 – arnoudhgz 2012-08-06 21:05:40

回答

1

緊接在另一個ON子句之後的是ON子句。基於在第二個指定的表/列,它看起來像你缺少一個加入那裏dle_post:

-- existing: 
LEFT JOIN dle_awards ON uid = dle_post ON dle_comments.post_id=dle_post.id 
-- becomes: 
LEFT JOIN dle_awards ON uid = dle_post LEFT JOIN dle_post ON dle_comments.post_id=dle_post.id 

當然,這可能需要調整,因爲它看起來並不像dle_post(在第一個ON子句)實際上是有效的。我需要看到架構知道。

+0

謝謝你,但是當我做了你曾經說過的話之後,我仍然有這個問題!我不知道,也許我不能理解你的提示。 – Alireza 2012-08-06 21:42:25

1
... ON uid = dle_post LEFT JOIN ON dle_comments.post_id=dle_post.id ... 

添加上面左邊的兩個之間的連接在「ON」的條款

1

查詢中的錯誤預期。在錯誤指向的查詢中,您有冗餘ON。

只要看看那裏的錯誤是:

SELECT dle_comments.id,POST_ID,dle_comments.user_id,dle_comments.date,dle_comments.autor爲gast_name,dle_comments.email爲gast_email,文本,IP,is_register,GROUP_CONCAT(中間)爲awards,名稱,dle_users.email,news_num,dle_users.comm_num,user_group,lastdate,reg_date,簽名,foto,全名,land,icq,dle_users.xfields,dle_post.title,dle_post.date as newsdate,dle_post.alt_name ,dle_post.category FROM
dle_comments LEFT JOIN dle_awards
ON UID = dle_post ON dle_comments.post_id = dle_post.id
LEFT JOIN dle_users ON dle_comments.user_id = dle_users.user_id ORDER BY ID DESC LIMIT 0,30

========== ===
在執行交接時,請參閱右表語法...從TABLE1左加JOB TABLE2 ON TABLE1.columnName = TABLE2.columnName。因此,在兩個連續的斜體塊中取出一個,並指定dle_comments中的一列和dle_awards中的另一列以用於剩餘的ON部分。