在 MySQL 查询中使用“distinct"

编程入门 行业动态 更新时间:2024-10-26 10:30:21
本文介绍了在 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"

本文发布于:2023-10-20 22:21:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1512311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL   distinct   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!