Create query without repeating column value

2

I am trying to make a query in which a value is not repeated since it is repeated several times. In my database, I have these data:

How can I generate a query so that the value of emitter is not repeated? For example, do not repeat the number 2?

so far my query is left as follows:

function obtener_mensajes($conexion, $us){
    $statement = $conexion->prepare("SELECT SQL_CALC_FOUND_ROWS * FROM mensajes WHERE receiver = $us AND emitter = emitter AND seen_empresa = 0 LIMIT 1 ");
    $statement->execute();
    return $statement->fetchAll();
}

Try with DISTINCT but not result

SOLUTION

Thanks to @ A.Cedano

function obtener_mensajes($conexion, $us){
    $statement = $conexion->prepare("SELECT SQL_CALC_FOUND_ROWS * 
        FROM mensajes WHERE receiver = $us AND seen_empresa = 
        0 GROUP BY emitter ORDER BY id ASC ");
    $statement->execute();
    return $statement->fetchAll();
}

in this way it stops repeating emitter

    
asked by Cesar Gutierrez Davalos 29.06.2017 в 02:53
source

1 answer

2

One option would be to use GROUP BY .

Example:

SELECT  emitter, reciever 
    FROM tabla_emitter GROUP BY emitter;

Result

    emitter    reciever
    1          1
    2          4
    3          7

And if you want an array of recievers related to that emitter. Combine GROUP_CONCAT with GROUP BY .

Example:

SELECT  emitter, GROUP_CONCAT(reciever SEPARATOR '|') as recivers 
    FROM tabla_emitter GROUP BY emitter;

Result

    emitter    recivers
    1          1|2|3
    2          4|5|6
    3          7

Ver DEMO

    
answered by 29.06.2017 / 03:18
source