0

我对这段代码有疑问:

$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

我该如何解决这个问题?

4

3 回答 3

1

您有一个紧跟在另一个 ON 子句之后的 ON 子句。根据第二个中指定的表/列,您似乎缺少一个 JOIN 到 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 子句中)实际上是有效的。我需要查看架构才能知道。

于 2012-08-06T21:05:04.487 回答
1
... ON uid = dle_post LEFT JOIN ON dle_comments.post_id=dle_post.id ...

在两个“ON”子句之间添加上面的 LEFT JOIN

于 2012-08-06T21:05:26.093 回答
1

您的查询中的错误是预期的。您在错误指向的查询中有多余的 ON 。

看看错误在哪里:

选择 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

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

于 2012-08-06T21:28:26.910 回答