MySQL Order by column = x, column asc? - mysql

I won't paste the whole query. It looks like this.
SELECT *, MONTH(date_created) as month
from table
GROUP BY month ORDER BY month='3', month asc
As it is April and I am querying this year I would have expected the order to be as follows 3, 1, 2, 4.
Instead I get out 1, 2, 4, 3.
How can I change the ORDER BY part of the statement to order the results by selected month first then the rest of months in the year showing sequentially?

add DESC
ORDER BY month = '3' DESC, month asc
month='3' is a boolean expression which returns 1 or 0, so basically when the result is zero, it will on the last part of the result.
or without using DESC, use <>
ORDER BY month <> '3', month asc

You have to add DESC or ASC in the first order :
SELECT *, MONTH(date_created) as month
FROM table
GROUP BY month ORDER BY month='3' DESC, month ASC

The solution is ORDER BY FIELD
SELECT * FROM table
GROUP BY month
ORDER BY FIELD(month, 3,1,2,4)

Related

Order by multiple columns with multiple conditions

I have a table with some events like this
id----------title-----------date-------------status
1-----------birthday-------2018-03-12--------1
2-----------match----------2018-03-13--------2
3-----------anniversary----2018-03-10--------1
4-----------trip-----------2018-03-15--------1
5-----------birthday-------2018-03-17--------2
6-----------birthday-------2018-03-11--------1
Expected Result
id----------title-----------date-------------status
1-----------birthday-------2018-03-12--------1
4-----------trip-----------2018-03-15--------1
5-----------birthday-------2018-03-17--------2
2-----------match----------2018-03-13--------2
6-----------birthday-------2018-03-11--------1
3-----------anniversary----2018-03-10--------1
I need to query it like the first rows which have dates greater than today with status 1 should appear first and then the rest in desc.
Suppose today is 2018-03-11 then row with id 1 should appear first and then the rest of the rows is desc order
This is what I have tried so far
SELECT *
FROM events
ORDER BY (date > CURDATE() and status = 1) asc,
date desc
You can use multiple keys in an order by:
order by (date >= curdate() and status = 1) desc,
date desc
I believe your SQL should be something like this but is hard to say without expected results.
Query
SELECT
*
FROM
[table]
WHERE
date > CURDATE()
AND
status = 1
ORDER BY
date ASC
LIMIT 1
UNION
SELECT
*
FROM
[table]
WHERE
id NOT IN (
SELECT
id
FROM
[table]
WHERE
date > CURDATE()
AND
status = 1
LIMIT 1
)
AND
date > CURDATE()
ORDER BY
date DESC

Select last n record within group by year month

I have a table where I need to get the last 12 records which is grouped by year() month() ASC. I try to use the query below but the result is not as expected.
SELECT * FROM
(
SELECT
id,
tanggal,
date_format(tanggal,'%b-%Y') as bulan,
sum(sisa_pokok) as jumlah
FROM transaksi_detail
GROUP BY date_format(tanggal,'%b-%Y')
ORDER BY id DESC LIMIT 12
) sub
ORDER BY id ASC
the query result is as below
My expected result is sort by bulan column order by year(), month() as follows
Bulan jumlah
Mar-2018 26600000
Oct-2017 1000000
Sept-2017 4500000
and so on....
EXTRACT(YEAR FROM bulan) as year
SELECT EXTRACT(YEAR FROM tanggal) as year , EXTRACT(MONTH FROM tanggal) as month, id FROM table_name group by year order by month
you can get year same like you can get month after that put group by and order i hope it will help you
This works for my situation
SELECT * FROM
(
SELECT
id,
tanggal,
month(tanggal),
year(tanggal),
date_format(tanggal,'%b-%Y') as bulan,
sum(sisa_pokok) as jumlah
FROM transaksi_detail
GROUP BY date_format(tanggal,'%b-%Y')
ORDER BY id DESC LIMIT 12
) sub
ORDER BY year(tanggal), month(tanggal) ASC

Result by server Date

how to add query result as current date and less days. do not want fixed days pl help in mysql
select
Winner,
Coupon,
DATE_FORMAT(date, '%d-%m-%Y') AS `Date`
FROM table2
ORDER BY DATE_FORMAT(date, '%d-%m-%Y')
LIMIT 1, 30
You can add a where clause to that statement and use <= now() to get the rows less than or equal to the current datetime.
select
Winner,
Coupon,
DATE_FORMAT(date, '%d-%m-%Y') AS `Date`
FROM table2
where datecolumn<=now()
ORDER BY DATE_FORMAT(date, '%d-%m-%Y')
You can use limit 1 to return just one row. http://dev.mysql.com/doc/refman/5.7/en/select.html
Your current limit statement, LIMIT 1, 30, is returning the second row though the thirty first row (the first value is the offset, the second is the number of rows).

Mysql query latest before date or (if empty then) first after

I'm trying to retrieve an entry with the latest date before a given date, and if it doesn't exist then take the soonest after that given date.
I tried googling it, but I couldn't find this scenario
You can do this with order by and limit:
select t.*
from table t
order by (datecol < #date) desc,
(case when datecol < #date then datecol end) desc,
datecol asc
limit 1;
for latest before given date -
select * from table where date_column<given_date order by date_column desc limit 1
now check the result if no row return then execute following query for soonest after given date -
select * from table where date_column>given_date order by date_column limit 1

Ordering results differently in MySQL

I have a table which has 3 columns. (day, month and year)
This statement gives results in chronological order.
SELECT * FROM table WHERE = 'condition' ORDER BY year, month, day
How can I get it in the inverse order?
You need to invert your sort order in your query:
SELECT * FROM table WHERE = 'condition' ORDER BY year DESC, month DESC, day DESC
Having separate columns for year, month, and day is counter-productive, though, as all of these could be represented in a singular DATE type column. This can be indexed and is much faster in practice:
SELECT * FROM table WHERE ... ORDER BY date_column DESC
Try this:
SELECT * FROM table
WHERE = 'condition'
ORDER BY year DESC, month DESC, day DESC
You can use the ASC or DESC keywords.
https://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html
For example:
SELECT *
FROM table WHERE = 'condition'
ORDER BY year DESC,
month DESC,
day DESC