Sort MySQL query for the last 3 months

2

I want to do an ordered SQL query for the first 3 months from the current month, for example:

Current month is August (08), so I order as:

10
9
8
1
12

I am using the following command:

SELECT * FROM 'tb_convencao' WHERE 'lg_historico' = 0 ORDER BY ('desc_database') >= MONTH(now()) DESC, ('desc_database') < date_add(MONTH(now()), interval 3 month)

But it is returned to me:

10
12
8
9
1

Any tips?

    
asked by anonymous 25.08.2015 / 19:10

1 answer

2

Apparently this is what you need but I can not guarantee that the question is confusing:

SELECT * FROM 'tb_convencao' WHERE 'lg_historico' = 0 AND
    'desc_database' >= MONTH(now()) - 3
    ORDER BY 'desc_database' DESC

If you want the next 3 months:

SELECT * FROM 'tb_convencao' WHERE 'lg_historico' = 0 AND
    'desc_database' >= MONTH(now()) AND
    'desc_database' <= MONTH(now()) + 3
    ORDER BY 'desc_database' DESC

If you want to show the next 3 months first and then everything else, make a selection with these months and then another selection with the others. If you want everything to be in one query, just use a UNION .

    
25.08.2015 / 19:26