在 MySQL 查询中使用“distinct" [英] Using 'distinct' in a MySQL query

查看:58
本文介绍了在 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.

这篇关于在 MySQL 查询中使用“distinct"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆