GROUP BY and ORDER BY MySQL [closed]

-2

When I send Query:

SELECT * FROM tabela WHERE id_cliente = '$id_cliente' ORDER BY ano DESC

Duplicate values are returned, because there are duplicate values in the table. But I want to return only once every year contained in the table.

More unsuccessful when submitting the query's below:

SELECT * FROM tabela WHERE id_cliente = '$id_cliente' GROUP BY ano ORDER BY ano DESC

SELECT * FROM tabela WHERE id_cliente = '$id_cliente' ORDER BY ano DESC GROUP BY ano

 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in [...]

Image of the bank.

Tablestructure: here

    
asked by anonymous 11.07.2017 / 20:08

4 answers

1

To bring every year of the user without repeating, you can do this:

SELECT DISTINCT ano, id, id_cliente, mes, status
FROM programacao_clientes_mes
WHERE id_cliente = 2
ORDER BY ano DESC
    
11.07.2017 / 21:00
0

If you do this by grouping by ID and year should come the expected result

SELECT  * FROM tabela WHERE id_cliente = '$id_cliente' GROUP BY ano,id_cliente ORDER BY ano,id_cliente DESC
    
11.07.2017 / 20:34
0

The order of the query is first GROUP BY and then ORDER BY

Ai would be SELECT * FROM tabela WHERE id_cliente = '$id_cliente' GROUP BY ano ORDER BY ano DESC

    
11.07.2017 / 20:45
0

I would do it as follows:

SELECT id, id_cliente, mes, status, ano FROM programacao_clientes_mes WHERE id_cliente = 2 GROUP BY mes, ano ORDER BY ano DESC
    
11.07.2017 / 21:09