Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

mysql - want to remove duplicates in SQL

I am creating an app with rails.
I'm stuck with how to write SQL statements, so please teach me.

want to

From this, narrow down by "competition_id" and extract only one item with the smallest "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 session_id item_id
1 1 2
2 1 3
2 1 2
2 1 2
2 1 5
3 1 7
4 1 4
5 1 5
question from:https://stackoverflow.com/questions/65865008/want-to-remove-duplicates-in-sql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should give you the wanted result, you must check if MIN is the right choice for every value

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...