how can I use group by next to order by

1

How to use GROUP BY along with ORDER BY ? I need to sort my tabela form ASC but I can not use GROUP BY how should I do to use the two together?

code snippet:

mysqli_query($conn, "SELECT nome_fotos, img, id_cliente FROM alboom WHERE id_fot = '$getid' GROUP BY id_cliente ORDER BY id ASC");

This is my code, but it does not sort the photos.

the result after the query in the workbench was

name_photos = merry img = merry.jpg customer_id = 2

or it did not order because I have id = 1 in the database.

    
asked by anonymous 04.05.2017 / 06:43

1 answer

0

Not tested, but I believe you can separate sorting into a subselect:

SELECT * FROM (
  SELECT nome_fotos, img, id_cliente, id_fot FROM alboom 
         WHERE id_fot = '$getid' ORDER BY id_cliente ASC
) AS tabela GROUP BY id_cliente
    
04.05.2017 / 07:11