如何排除某一字段中有重复项的行

发布于 2024-09-11 11:19:39 字数 673 浏览 8 评论 0原文

我有一个非常简单的任务,但我找不到任何解决方案。我有两个表,“文章”和“类别”

我的文章表如下所示:

id | cat_id | title | content
1      1      Blah     Content 1
2      1      Blah2    Content 2
3      2      Blah3    Content 3

我的类别表如下所示:

id | title
1     Category 1
2     Category 2

您看,我有 2 篇具有相同 cat_id 的文章。我不想使用重复的 cat_id 字段。我不能使用 DISTINCT,因为我将获取所有文章,因为我想要所有字段。

所以如果我像这样使用 DISTINCT:

SELECT DISTINCT a.id, a.cat_id, a.title, a.content FROMarticles AS a

我会得到所有内容,但我想要这样的输出

id | cat_id | title | content
2      1      Blah2    Content 2
3      2      Blah3    Content 3

有人可以帮助我吗!

I have a very simple task, but I cannot find any solution. I have two tables, 'articles' and 'categories'

My article table look like this:

id | cat_id | title | content
1      1      Blah     Content 1
2      1      Blah2    Content 2
3      2      Blah3    Content 3

My categories table look like this:

id | title
1     Category 1
2     Category 2

You see I have 2 articles that have the same cat_id. I do not want with duplicate cat_id field. I cannot use DISTINCT, because I will get all articles, because I want all fields out.

so if i use DISTINCT like this:

SELECT DISTINCT a.id, a.cat_id, a.title, a.content FROM articles AS a

I will get everything out, but I want output like this

id | cat_id | title | content
2      1      Blah2    Content 2
3      2      Blah3    Content 3

Can someone help me please !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡风轻 2024-09-18 11:19:39

此查询将从每个类别中选择第一篇文章(最低 ID),

SELECT a.* FROM Article a LEFT JOIN 
   Article a2 ON a.id<a2.id AND a.cat_id=a2.cat_id
WHERE a2.id IS NULL

它与所有其他文章进行外连接。 join 子句仅连接具有相同类别且 ID 较小的文章。如果没有匹配项(a2.id 为 NULL),则我们将获得该类别 ID 最小的文章。

This query will select the first article (lowest id) from each category

SELECT a.* FROM Article a LEFT JOIN 
   Article a2 ON a.id<a2.id AND a.cat_id=a2.cat_id
WHERE a2.id IS NULL

It does an outer join with all other articles. The join clause only joins articles with the same category and with a smaller ID. When there are no matches (a2.id is NULL), then we have the article with the lowest ID for that category.

二智少女 2024-09-18 11:19:39

试试这个:

select article.*
    from article
        join (select min(id) as id, cat_id from article group by cat_id) a2
            using (id);

或者:

select *
    from article
    where id in (select min(id) from article group by cat_id);

两者都为每个不同的 cat_id 选择一个文章 id(使用 min()),并仅选择具有这些 id 的记录。

Try this:

select article.*
    from article
        join (select min(id) as id, cat_id from article group by cat_id) a2
            using (id);

or:

select *
    from article
    where id in (select min(id) from article group by cat_id);

Both select one article id (using min()) for each distinct cat_id and select only the records with these ids.

云醉月微眠 2024-09-18 11:19:39

未经测试,但应该可以工作:

; WITH cte AS (
   SELECT
      *
      ,ROW_NUMBER() OVER (PARTITION BY cat_id ORDER BY id DESC) AS RowNum
)
SELECT
   *
FROM cte
WHERE RowNum = 1;

Not tested, but should work:

; WITH cte AS (
   SELECT
      *
      ,ROW_NUMBER() OVER (PARTITION BY cat_id ORDER BY id DESC) AS RowNum
)
SELECT
   *
FROM cte
WHERE RowNum = 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文