I have a mysql query that is returning the following data:
select Desc,Value from table;
Desc Value
a 4
r 3
e 4
j 7
w 6
p 6
I want to order by Descr but in a specific order (This specific is required for json)
Desc Value
r 3
a 4
e 4
j 7
p 6
w 6
Is this possible?
ORDER BY FIELD(`desc`, 'r', 'a', 'e', 'j', 'p', 'w')
Note: if desc value doesn't match the given values, it will come first, as FIELD() would return 0
It's not pretty, but you can order by (condition) DESC. DESC because if the condition is true it's more than if the condition is false:
SELECT
Desc,
Value
FROM
Foo
ORDER BY
(Desc = 'r') DESC,
(Desc = 'a') DESC,
(Desc = 'e') DESC,
(Desc = 'j') DESC,
(Desc = 'p') DESC,
(Desc = 'w') DESC
You could order by a case expression that specifies the ordering you want:
SELECT `desc`, `value`
FROM my_table
ORDER BY CASE `desc` WHEN 'r' THEN 1
WHEN 'a' THEN 2
WHEN 'e' THEN 3
WHEN 'j' THEN 4
WHEN 'p' THEN 5
WHEN 'w' THEN 6
ELSE 999 --for completeness sake
END CASE ASC
Related
Here is the mysql query:
SELECT `listings`.*
FROM `listings`
WHERE `listings`.`site_id` = '2'
AND `listings`.`type_id` = '2'
GROUP BY `listings`.`book_id`
ORDER BY
(CASE WHEN DATE(`listings`.`extended_end_datetime`) < DATE(CURDATE())
THEN 1
ELSE 0
END) ASC,
`listings`.`extended_end_datetime` DESC ,
`listings`.`dates_tentative` asc;
Here is how it currently sorts:
Here is how I would like it to sort:
For this sample data this will work:
order by
extended_end_datetime < curdate(),
abs(timestampdiff(minute, extended_end_datetime, now())) ,
dates_tentative
See a simplified demo.
SELECT `listings`.*
FROM `listings`
WHERE `listings`.`site_id` = '2' AND `listings`.`type_id` = '2'
GROUP BY `listings`.`book_id`
ORDER BY
(CASE WHEN DATE(`listings`.`extended_end_datetime`) < DATE(CURDATE()) THEN -1 ELSE 1 END) *
TIMESTAMPDIFF(second, DATE(`listings`.`extended_end_datetime`), DATE(CURDATE()),
`listings`.`extended_end_datetime` DESC ,
`listings`.`dates_tentative` asc
It calculate the time difference between today, and the date in the DB, and multiplies it with 1 or -1 depending on how you want to sort.
I have a situation here, I am having a join of two tables to get records, where one table is storing key value pair in two different columns(wordpress user meta table).
So heres my query:
SELECT
um.user_id
FROM
sl_job_applications as ja,
sl_usermeta as um
WHERE
um.user_id = ja.user_id
AND ja.job_id = 3
AND ja.STAGE = 'Application'
AND ja.STATUS = 'In progress'
group by ja.user_id
order by case when (um.meta_key = 'CURRENT_TOTAL_EXPERIENCE') then -1 else 2 end,
um.meta_value asc
LIMIT 0 , 50;
The order by is not working here, my data is
user_id meta_key meta_value
3 CURRENT_TOTAL_EXPERIENCE 6
4 CURRENT_TOTAL_EXPERIENCE 2
5 CURRENT_TOTAL_EXPERIENCE 1
6
I hope you understand my table data,
My above query returns
6,4,5,3
But I am expecting this output:
6,5,4,3
Simply change the order by to have an aggregation function such as max() and the value:
order by min(case when (um.meta_key = 'CURRENT_TOTAL_EXPERIENCE') then -1 else 2 end),
min(case when (um.meta_key = 'CURRENT_TOTAL_EXPERIENCE') then um.meta_value end) asc
LIMIT 0 , 50;
The first checks that the meta_key with that value exists. The second extracts the value and does the sort.
Try this:
SELECT um.user_id
FROM sl_job_applications AS ja
INNER JOIN sl_usermeta AS um ON um.user_id = ja.user_id
WHERE ja.job_id = 3 AND ja.STAGE = 'Application' AND ja.STATUS = 'In progress'
GROUP BY ja.user_id
ORDER BY CASE WHEN (um.meta_key = 'CURRENT_TOTAL_EXPERIENCE') THEN -1 ELSE 2 END,
CAST(um.meta_value AS SIGNED) ASC
LIMIT 0, 50
I want to order grades that are a string in the following order.
K, 1, 2, 4, .... 12
But I keep getting 5, 4, 3, ... 12, K, 1
The grade column in sims_classroom is VARCHAR(255) and the table is latin1;
I have tried every cast and trick in the book. At the moment I have the following trick by adding 0 to it. What am I doing wrong?
SELECT
(SELECT district_get_name_function(sod.parent_id, sod.id)) AS 'district_name',
(SELECT school_get_name_function(so.id)) AS 'school_name',
st.teacher_username,
st.teacher_first_name,
st.teacher_last_name,
c.name as 'classroom_name',
c.grade,
c.id,
d.name AS 'discipline_name',
d.id AS 'discipline_id',
lc.name AS 'program_name',
lc.id AS 'program_id'
FROM sims_classroom c
.
.
.
ORDER BY
CASE lower(sort_direction) WHEN 'asc' THEN
CASE lower(sort_order)
WHEN 'grade' THEN
CASE c.grade
WHEN 'K' THEN 0
ELSE (c.grade + 0)
END
WHEN 'teachername' THEN lower(st.teacher_first_name)
ELSE c.name
END
END ASC,
CASE lower(sort_direction) WHEN 'desc' THEN
CASE lower(sort_order)
WHEN 'grade' THEN
CASE c.grade
WHEN 'K' THEN 10000
ELSE (c.grade + 0)
END
WHEN 'teachername' THEN lower(st.teacher_first_name)
ELSE c.name
END
END DESC
You could create a lookup table for the grades. It would have the Grade_Name (i.e. 'K','1','2', etc) and a Grade_Order as an int.
You'd have records like this:
Grade_Name Grade_Order
K 1
1 2
2 3
3 4
and so on.
Then you can add that lookup table to your JOIN and ORDER BY Grade_Order.
I could only get it to work doing this.
WHEN 'grade' THEN
CASE c.grade
WHEN 'K' THEN '00'
WHEN '9' THEN '09'
WHEN '8' THEN '08'
WHEN '7' THEN '07'
WHEN '6' THEN '06'
WHEN '5' THEN '05'
WHEN '4' THEN '04'
WHEN '3' THEN '03'
WHEN '2' THEN '02'
WHEN '1-2' THEN '01'
WHEN '1' THEN '01'
ELSE c.grade
END
I am writing a query to get the top 10 rated businesses, the number of positive comments for each business, the number of negative comments for each business and the latest comment for each of these businesses.
SELECT comment.bis_id, Sum( Case When comment.rating <= 2 Then 1 Else 0 End ) As NegVotes
, Sum( Case When comment.rating >= 4 Then 1 Else 0 End ) As PosVotes, bis.bis_name
FROM bis, comment
WHERE comment.bis_id = bis.bis_id
GROUP BY bis_id
ORDER BY PosVotes DESC
LIMIT 0, 10";
The above gets positive comments and negative comments, but I can't seem to work out how to get the latest comment as well.
SELECT
c.bis_id
, Sum( Case When c.rating <= 2 Then 1 Else 0 End ) As NegVotes
, Sum( Case When c.rating >= 4 Then 1 Else 0 End ) As PosVotes
, b.bis_name
, cc.last_comment
FROM bis b
INNER JOIN comment c on (c.bis_id = b.bis_id)
INNER JOIN (SELECT c2.bis_id, c2.comment_text as last_comment
FROM comment c2
GROUP BY c2.bis_id
HAVING c2.comment_date = MAX(c2.comment_date) ) cc
ON (cc.bis_id = b.bis_id)
GROUP BY b.bis_id
ORDER BY PosVotes DESC
LIMIT 10 OFFSET 0
I want to have my table,rcarddet, ordered by "SDNO" (not primary key) in ascending order with the exception of "0". So it should turn out to be like:
1
1
2
.
.
10
0
0
My query now is:
SELECT *
FROM `rcarddet`
WHERE `RDATE` = '2011-05-25'
AND `RCNO` = '1'
AND `PLACE` = 'H'
AND `SDNO` != 0
ORDER BY `rcarddet`.`SDNO` ASC;
The easiest way
SELECT * FROM rcarddet
WHERE RDATE = '2011-05-25' and RCNO = '1'and PLACE = 'H'
ORDER BY CASE
WHEN rcarddet.SDNO = 0 THEN [max_number_for_the_type_of_SDNO]
ELSE rcarddet.SDNO
END ASC
SELECT *
FROM `rcarddet`
WHERE `RDATE` = '2011-05-25'
AND `RCNO` = '1'
AND `PLACE` = 'H'
ORDER BY
`SDNO` = 0,
`SDNO`;