SQL See DB count

1

I have a table called decree in the database with the following fields:

and I want to see the years that I have in my database in which I have entered data. Example in the database I entered dates in 2016 2017 2018 and the query that shows me the values 2018 - 2017 - 2016. I have the following query but it does not work very well.

SELECT fecha
    FROM decreto
    WHERE fecha
    IN (
    SELECT fecha
    FROM decreto
    GROUP BY fecha
    HAVING count( fecha ) >1
    )
    ORDER BY fecha

I hope I can explain myself well

    
asked by MoteCL 20.09.2017 в 15:35
source

2 answers

2

If your problem is that the order is not correct, the problem is that by default, mysql orders the results ascending, you only have to add DESC at the end of the query, like this:

SELECT fecha
    FROM decreto
    WHERE fecha
    IN (
    SELECT fecha
    FROM decreto
    GROUP BY fecha
    HAVING count( fecha ) >1
    )
    ORDER BY fecha DESC;

Edit: To show the dates without duplicates, simply add a Distinct of the date, the query would look something like this:

SELECT DISTINCT(fecha)
    FROM decreto
    WHERE fecha
    IN (
    SELECT fecha
    FROM decreto
    GROUP BY fecha
    HAVING count( fecha ) >1
    )
    ORDER BY fecha DESC;
    
answered by 20.09.2017 / 15:42
source
0

You could do SELECT UNIQUE date FROM decree WHERE date IS NOT NULL;

    
answered by 20.09.2017 в 15:46