I have a table in a MySQL database.
One of the columns is entitled Failed and some rows have the value Yes in them.
How do I order my SELECT query so it shows the rows that have no value first and then shows the ones with Yes as the value at the end?
Just use ASC or DESC when ordering on the field(s) you wish to order by. In this case try:
ORDER BY Failed ASC
Use CASE in Order by
Order by case when col <> 'yes' then 2 else 1 end
Related
I have the following MySql Query:
SELECT * FROM myTable ORDER BY FIELD(
priority,1,2,3,4,5,6,7,8,....,300,0),date_fixed ASC, sno DESC";
In fact, i need to display record ordered by priority ASC but also want to keep all the record having the priority = 0 at the end of the any other numerical value. The above query working fine but I think its not worth to write values starting from 1 to 300 and at the end i just put 0.
My Question is that any shortcut way to achieve the ResultSet as Orderby priority ASC and keeping the 0th priorities at the end?
I have a table with the following columns in it:
status
scheduled_start
I would like to sort it to show rows with the status set to "Needs Attention" first, but I want a secondary sort by scheduled_start in ASC order.
Doing:
SELECT `tickets`.* FROM `tickets` ORDER BY CASE status WHEN 'Needs Attention' THEN 0 ELSE 1 END AND scheduled_start ASC
Will produce the rows sorted by status = Needs Attention, but the secondary sort by scheduled start is not working.
Any ideas?
You have an unnecessary "AND" in the ORDER BY clause. Try replacing it with a comma instead:
SELECT `tickets`.*
FROM `tickets`
ORDER BY
CASE status WHEN 'Needs Attention' THEN 0 ELSE 1 END,
scheduled_start ASC
Try this:
SELECT `tickets`.* FROM `tickets` ORDER BY CASE WHEN `status` = 'Needs Attention' THEN 0 ELSE 1 END ASC, scheduled_start ASC
Order by clause is not a condition, it is a list of fields and expressions you want to sort on, so separate them by commas, not an and:
.... order by expression1, field1
I have a table with a column priority_n. Pretend there are 5 items in that table. Two with nil as priority_n, and the other three have 1, 2, 3.
I'd like to do a where(priority_n: nil).order(published_at: :desc) combined with where.not(priority_n: nil).order(priority_n: :asc). I want the nil ones at the beginning of the active record relations, and then the prioritized ones after them. Is there a way to do this?
If I could figure out how to do this in SQL then I could do it in rails.
The following is the order by clause in standard SQL:
order by (case when priority_n is null then 0 else 1 end),
priority_n asc
Case statements will not make efficient use of indexes.
ORDER BY priority_N IS NULL DESC, priorit_n ASC
In PostgreSQL, sorting nulls first / last is dead simple with the (standard SQL!) NULLS FIRST | LAST:
ORDER BY priority_n NULLS FIRST, published_at
The second ORDER BY item, because it seems you want to order rows with the same priority_n according to published_at.
PostgreSQL sort by datetime asc, null first?
ORDER BY DATE showing NULLS first then most recent dates
MySQL does not implement NULLS FIRST | LAST. Substitute with:
ORDER BY priority_n IS NOT NULL, priority_n, published_at
Would work in Postgres, too.
priority_n IS NOT NULL is a boolean expression that evaluates to FALSE (0) or TRUE (1). 0 sorts before 1 (and both before NULL, but not relevant here.), so rows with priority_n IS NULL come first.
ORDER BY ASC with Nulls at the Bottom
MySQL Order by column = x, column asc?
I need to be able to display the results of the below query in a specific order. For example: showing featured listings before the rest of the results.
WHERE IS `featured-listing` && WHERE IS NOT `featured-listing`
Could probably run 2 queries and a union right, bu is that the most effective solution? I know this can be done with one query I just cant remember how it/s done. Any and all help is appreciated.
SELECT `Assigned-Regions`,`Description`,`Category`,`Start-Date` FROM `adds` WHERE `Status` = "Active" ORDER BY `Start-Date` DESC
I would use a case statement for ORDER BY.
So something like
SELECT ... ORDER BY (CASE WHEN featured-listing THEN 1 ELSE 2) ASC, some-other-field ASC
Sounds like all you need is to add an ORDER BY clause to your query.
If featured-listing column is integer datatype and contains values of 1 or 0 (1=is featured listing, 0=not a featured listing), then you could simply add something as simple as:
ORDER BY `featured-listing` DESC, `Start-Date` DESC
Or, you could use an expression:
ORDER BY IF(`featured-listing`=1,1,0) DESC, `Start-Date` DESC
you can do conditional ordering.. not sure what featured-listing is without seeing some data but this is the logic for conditional ordering
SELECT `Assigned-Regions`,`Description`,`Category`,`Start-Date`
FROM `adds`
WHERE `Status` = "Active"
ORDER BY
CASE WHEN `featured-listing` THEN 1 ELSE 2 END ASC,
`Start-Date` DESC
I have a MySQL database and a have a funny question.
I need to order the results of a query by a field which has entries of 1,2,3 or 4, ordered descending but with 4 at the end.
So I wish to have the results in the following order
3's
2's
1's
4's
Is this possible at all?
I know I can do order the result array in php but unfortunately I need to do this in an sql statement.
If the field is an int,
ORDER BY (fieldname = 4) DESC, fieldname DESC
should do the trick.
Here is another cool way
ORDER BY MOD(fieldname,4) DESC,fieldname
If the result is a CHAR(1) then
ORDER BY LOCATE(fieldname,'3214'),fieldname
add this to the order
ORDER BY
CASE field_name WHEN 4 THEN 1
ELSE 2
END
this will return the result of the query order using the value of the field