简体   繁体   中英

MySQL INNER JOIN multiple rows same value

Both of the following tables have 2 entries for the same player.

Table punishment: 表罚 Table cooldown: 桌子冷却时间

SELECT punishment_id, punishment.reason, cooldown.id, cooldown.time_left FROM player INNER JOIN punishment ON player.most_uuid=punishment.most_uuid INNER JOIN cooldown ON player.most_uuid=cooldown.most_uuid WHERE player.most_uuid=2941535451391147758

In the result of this query I have some kind of "duplicated" rows. I get 4 rows when I'd like to have just 2 with the essentials values inside.

Result from the query查询结果

I need all the values present in the rows, but as you can see there are pairs of rows with the exact same value twice.

Is there a possibility of merging them to actually have 2 like this (and not having duplicated values):

在此处输入图像描述

I think:

SELECT punishment_id, punishment.reason, cooldown.id, cooldown.time_left FROM player INNER JOIN punishment ON player.most_uuid=punishment.most_uuid INNER JOIN cooldown ON player.most_uuid=cooldown.most_uuid WHERE player.most_uuid=294153545139114775

Can be transform in:

SELECT punishment_id, punishment.reason, cooldown.id, cooldown.time_left FROM punishment INNER JOIN cooldown ON punishment.most_uuid=cooldown.most_uuid WHERE punishment.most_uuid=294153545139114775

This only work if you don't want to check existence on player table.

If you did it on purpose, I recommend using GROUP BY:

SELECT punishment_id, punishment.reason, cooldown.id, cooldown.time_left FROM player INNER JOIN punishment ON player.most_uuid=punishment.most_uuid INNER JOIN cooldown ON player.most_uuid=cooldown.most_uuid WHERE player.most_uuid=294153545139114775 GROUP BY punishment_id

I hope this is usefull. Bye

You have four rows because each pair have different cooldown.id and cooldown.time_left (3, 2019-11-05 19:10:08 and 5, 2019-11-05 19:09:50 ) so if this column is not important for you couldl, instead of select *.. use a select with explcit column name and an aggregation function for cooldown.id and time_left eg min(cooldown.id ), min(cooldown.time_left)

    SELECT a.*, b.* c.least_id, min(id), min(time_left)
    FROM player a
    INNER JOIN punishment b ON a.most_uuid=b.most_uuid 
    INNER JOIN cooldown c  ON a.most_uuid=c.most_uuid 
    WHERE a.most_uuid=2941535451391147758
    group by a.most_uuid,  b.punishment_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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