In mysql it is possible to create an extra line if 2 or more rows are the same [closed]

0

Hello, I would like to know if it is possible to create a fourth line for mysql based on Titles that are repeated as in the first table where the game The Witcher® 3: Wild Hunt repeats 3 times and is generated on 4 lines based on second table?

<table>
    <thead>
        <tr>
            <th>Título</th>
            <th>Plataforma </th>
            <th>Preço</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>The Witcher 3: Wild Hunt</td>
            <td>Pc</td>
            <td>R$ 50</td>          
        </tr>
        <tr>
            <td>The Witcher 3: Wild Hunt</td>
            <td>XOne</td>
            <td>R$ 67</td>          
        </tr>
        <tr>
            <td>The Witcher 3: Wild Hunt</td>
            <td>Ps4</td>
            <td>R$  80</td>     
        </tr>
    </tbody>    
</table>
<h1>Tabela de Exemplo abaixo</h1>
<table>
<thead>
    <tr>
        <th>Título</th>
        <th>Plataforma </th>
        <th>Preço</th>
    </tr>
    <tbody>
        <tr>
            <td>The Witcher 3: Wild Hunt</td>
            <td>Pc/XOne/Ps4</td>
            <td colspan="3" >R$ 50(PC) R$ 67(XOne) R$ 80(Ps4)</td>      
        </tr>
</thead>
</table>
    
asked by anonymous 19.08.2017 / 13:24

1 answer

1

Hello,

You need to group the columns by the game title, in this case just use GROUP BY together with GROUP_CONCAT .

Ex:

SELECT
    titulo,
    GROUP_CONCAT(plataforma SEPARATOR ', ') AS plataforma,
    GROUP_CONCAT(preco SEPARATOR ', ') AS preco
FROM
    jogos
GROUP BY
    titulo;

If you want to bring the records separated and in the end, in the same query, bring a grouped row, you can do the two queries and join by UNION .

Ex:

SELECT
    titulo,
    plataforma,
    preco
FROM
    jogos
UNION ALL
SELECT
    titulo,
    GROUP_CONCAT(plataforma SEPARATOR ', ') AS plataforma,
    GROUP_CONCAT(preco SEPARATOR ', ') AS preco
FROM
    jogos
GROUP BY
    titulo;

Here's a fiddle with these examples in action: link

Read more about: GROUP BY GROUP_CONCAT UNION

    
19.08.2017 / 15:59