在 MySQL 查询中使用“distinct"
我有以下查询.
SELECT p.author_name, p.author_id,
DISTINCT p.topic_id, t.title
FROM `ibf_posts` p, `ibf_topics` t
WHERE p.topic_id = t.tid
ORDER BY pid DESC
LIMIT 8"
当我运行它时,出现以下 MySQL 错误:
When I run it, I get the following MySQL Error:
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 'DISTINCT p.topic_id, t.title FROM `ibf_posts` p, `ibf_topics` t WHERE p' at line 1
如果我删除 DISTINCT
关键字,那么查询就没有问题了.
if I remove the DISTINCT
keyword, then the query works without a problem.
我做错了什么?
此方案来自 Invision Power Board 帖子和主题表.我正在尝试获取具有最新帖子的最后 8 个主题的标题.在最新帖子列表中,我不希望同一主题出现多次.我想要一个独特的标题列表.
This scheme is from Invision Power Board post and topic table. I am trying to get the title of the last 8 topics which has the newest posts. In the list of top latest posts, I don't want the same topic to appear more than once. I want a unique list of titles.
表:ibf_posts-pid-作者姓名-author_id-topic_id
table: ibf_posts -pid -author_name -author_id -topic_id
表:ibf_topics-tid-标题
table: ibf_topics -tid -title
tid 与 topic_id 相同
tid is same as topic_id
It's
SELECT DISTINCT ...
不能只为单列指定DISTINCT
;它仅适用于从结果集中保留完整的重复记录.
You can't specify DISTINCT
only for a single column; it only works for keeping complete duplicate records out of the result set.