gpt4 book ai didi

mysql - 在 MySQL 查询中使用 'distinct'

转载 作者:行者123 更新时间:2023-12-01 00:16:43 24 4
gpt4 key购买 nike

我有以下查询。

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 个主题的标题。在最新帖子列表中,我不希望同一主题出现多次。我想要一个独特的标题列表。

表:ibf_posts-pid-作者姓名-author_id-topic_id

表:ibf_topics-tid-标题

tid 与 topic_id 相同

最佳答案

SELECT DISTINCT ...

您不能只为单个列指定 DISTINCT;它仅适用于将完整的重复记录保留在结果集中。

关于mysql - 在 MySQL 查询中使用 'distinct',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/502094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com