简体   繁体   English

想要删除 SQL 中的重复项

[英]want to remove duplicates in SQL

I am creating an app with rails.我正在用 rails 创建一个应用程序。
I'm stuck with how to write SQL statements, so please teach me.我不知道如何编写 SQL 语句,所以请教我。

want to想要

From this, narrow down by "competition_id" and extract only one item with the smallest "id".由此,通过“competition_id”缩小范围并仅提取具有最小“id”的项目。

For example,例如,

+----------------+----+---------------------------+----------------+-------+-----+
| competition_id | id | image                     | name           | count | rnk |
+----------------+----+---------------------------+----------------+-------+-----+
|              1 |  4 | monster4.jpeg             | monster4       |     7 |   1 |
|              2 |  6 | monster2.jpeg             | monster2       |     1 |   1 |
|              3 |  9 | monster1.jpeg             | monster1       |     1 |   1 |
|              5 | 22 | drink_sample.jpeg         | drink6         |     2 |   1 |
|              6 | 33 | sumo_wrestler_sample.jpeg | sumo_wrestler7 |     2 |   1 |
|              7 | 40 | movie_sample.jpeg         | movie4         |     2 |   1 |
|              8 | 50 | food_sample.jpeg          | food4          |     2 |   1 |
|              9 | 61 | color_sample.jpeg         | color5         |     3 |   1 |
|             10 | 72 | book_sample.jpeg          | book6          |     2 |   1 |
|             11 | 82 | book_sample.jpeg          | book6          |     3 |   1 |
+----------------+----+---------------------------+----------------+-------+-----+

Current status当前状态

+----------------+----+---------------------------+----------------+-------+-----+
| competition_id | id | image                     | name           | count | rnk |
+----------------+----+---------------------------+----------------+-------+-----+
|              1 |  4 | monster4.jpeg             | monster4       |     7 |   1 |
|              2 |  6 | monster2.jpeg             | monster2       |     1 |   1 |
|              3 |  9 | monster1.jpeg             | monster1       |     1 |   1 |
|              5 | 22 | drink_sample.jpeg         | drink6         |     2 |   1 |
|              6 | 33 | sumo_wrestler_sample.jpeg | sumo_wrestler7 |     2 |   1 |
|              6 | 34 | sumo_wrestler_sample.jpeg | sumo_wrestler8 |     2 |   1 |
|              6 | 35 | sumo_wrestler_sample.jpeg | sumo_wrestler9 |     2 |   1 |
|              7 | 40 | movie_sample.jpeg         | movie4         |     2 |   1 |
|              7 | 43 | movie_sample.jpeg         | movie7         |     2 |   1 |
|              7 | 45 | movie_sample.jpeg         | movie9         |     2 |   1 |
|              8 | 50 | food_sample.jpeg          | food4          |     2 |   1 |
|              8 | 56 | food_sample.jpeg          | food10         |     2 |   1 |
|              9 | 61 | color_sample.jpeg         | color5         |     3 |   1 |
|             10 | 72 | book_sample.jpeg          | book6          |     2 |   1 |
|             11 | 82 | book_sample.jpeg          | book6          |     3 |   1 |
+----------------+----+---------------------------+----------------+-------+-----+

The code that extracted this提取这个的代码

SELECT * FROM (SELECT *,RANK() OVER (PARTITION BY competition_id ORDER BY COUNT DESC) rnk FROM (SELECT items.competition_id,items.id,items.image,items.name,count(*) AS count FROM chosenitems INNER JOIN items ON chosenitems.item_id = items.id GROUP BY items.competition_id,items.id) AS t) AS tt WHERE rnk = 1;

Table structure表结构

  • chosenitems table选定项表
id ID session_id session_id item_id item_id
1 1 1 1 2 2
2 2 1 1 3 3
2 2 1 1 2 2
2 2 1 1 2 2
2 2 1 1 5 5
3 3 1 1 7 7
4 4 1 1 4 4
5 5 1 1 5 5
  • items table项目表
id ID name姓名 image图片 competition_id比赛编号
1 1 a一个 image1图像1 1 1
2 2 b b image2图2 1 1
2 2 c c image2图2 1 1
2 2 d d image2图2 1 1
2 2 e e image5图5 2 2
3 3 f F image9图 9 2 2
4 4 g G image4图4 2 2
5 5 h H image5图5 2 2

I tried我试过了

SELECT * FROM (SELECT *,RANK() OVER (PARTITION BY competition_id ORDER BY COUNT DESC) rnk FROM (SELECT items.competition_id,items.id,items.image,items.name,count(*) AS count FROM chosenitems INNER JOIN items ON chosenitems.item_id = items.id GROUP BY items.competition_id,items.id) AS t) AS tt WHERE id in (SELECT MIN(id) FROM chosenitems GROUP BY competition_id);

Failure失败

+----------------+----+---------------+----------+-------+-----+
| competition_id | id | image         | name     | count | rnk |
+----------------+----+---------------+----------+-------+-----+
|              1 |  1 | monster1.jpeg | monster1 |     4 |   2 |
+----------------+----+---------------+----------+-------+-----+

I want to extract the one with the smallest id from each "competition_id" where "rnk" is 1.我想从“rnk”为1的每个“competition_id”中提取ID最小的那个。

environment环境

macOS BigSur macOS 大苏尔
ruby 2.7.0 ruby 2.7.0
Rails 6.1.1导轨 6.1.1
mysql Ver 8.0.23 mysql 版本 8.0.23

This should give you the wanted result, you must check if MIN is the right choice for every value这应该会给您想要的结果,您必须检查MIN是否是每个值的正确选择

SELECT 
    competition_id
    ,MIN(id) as id
    ,MIN(mage) as image
    ,MIN(ame) as name
    ,MIN(count) as count
   ,MIN(rnk) as rnk
FROM 
    (SELECT 
        *
        ,RANK() OVER (PARTITION BY competition_id ORDER BY COUNT DESC) rnk 
    FROM (SELECT 
            items.competition_id
            ,items.id
            ,items.image
            ,items.name
            ,count(*) AS count 
        FROM 
            chosenitems INNER JOIN items ON chosenitems.item_id = items.id 
            GROUP BY items.competition_id,items.id) AS t) AS tt 
WHERE rnk = 1
GROUP BY competition_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM