SQL语法和SQL Server版本错误

时间:2012-08-06 20:44:09

标签: mysql syntax

我对此代码有疑问:

$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

我如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

在紧跟另一个ON子句之后,你有一个ON子句。基于第二个表中指定的表/列,看起来你在那里缺少对dle_post的JOIN:

-- 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子句中)实际上是有效的。我需要看到架构才能知道。

答案 1 :(得分:1)

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

在两个'ON'子句

之间添加上述LEFT JOIN

答案 2 :(得分:1)

您的查询中出现错误。您的查询中有错误指向的冗余ON。

只需查看错误的位置:

选择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(MID)为awards,姓名,dle_users .email,news_num,dle_users.comm_num,USER_GROUP,lastdate,reg_date,签名,照片,全名,土地,ICQ,dle_users.xfields,dle_post.title,dle_post.date为newsdate,dle_post.alt_name,dle_post.category  来自 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

=============
在执行联结时,请参阅表1中的正确语法... LEFT JOIN TABLE2 ON TABLE1.columnName = TABLE2.columnName。因此,在斜体块的两个连续ON中删除一个并从dle_comments指定一列,从dle_awards指定另一列以在剩余的ON部分中使用。