我有以下查询.
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错误:
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
关键字,那么查询工作没有问题.
我究竟做错了什么?
该方案来自Invision Power Board帖子和主题表.我想获得最新帖子的最后8个主题的标题.在最新的帖子列表中,我不希望同一主题出现多次.我想要一个独特的标题列表.
table:ibf_posts -pid -author_name -author_id -topic_id
table:ibf_topics -tid -title
tid与topic_id相同
它的
SELECT DISTINCT ...
您不能DISTINCT
仅指定单个列; 它只适用于保留结果集中的完整重复记录.