I have a column called "menu_order" which has no default value. When I select the contents of this column using the following select statement:
SELECT * FROM categories ORDER BY menu_order ASC
It lists the category items that have nothing as their menu order first and then the one's that have 1's and 2's and 3's. Is there any way to prevent SQL from take nothing to come before numbers when I am trying to list things in order?
So for example, if I have:
cat_name | menu_order
----------------------
Lunch | 1
Dinner |
And I perform my query, the output should be:
Lunch Dinner
Not:
Dinner Lunch
This will put the null values last:
SELECT *
FROM categories
ORDER BY menu_order IS NULL ASC, menu_order ASC
You can use the IFNULL function in the order by with a large number
ORDER BY IFNULL(menu_order, 1000) ASC
You can even try using a case statement
ORDER BY
CASE
WHEN menu_order IS NULL THEN 1
ELSE 0
END ASC,
menu_order ASC
ORDER BY IFNULL(menu_order, 99999999) ASC
Related
I have the following list of items
Category
OrderNum
Prerequisites
2
NULL
4
Prerequisites
6
Sign Off
8
Sign Off
10
I would like it to be ordered so that 'Prerequisites' is together and the NULL category appears after it, so that:
Category
OrderNum
Prerequisites
2
Prerequisites
6
NULL
4
Sign Off
8
Sign Off
10
Currently my SQL has the following order by:
ORDER BY OrderNum <> '' DESC, OrderNum
I've tried the following, however it puts NULL at the end.
ORDER BY COALESCE(Category,'') <> '' DESC, OrderNum <> '' DESC, OrderNum
I'm trying to achieve it so that the records with the same category are together in the recordset, the NULL item should appear before the 'Sign Off' category because the OrderNum of NULL is less than any of the 'Sign Off' records.
I'm not sure if that's possible in one query. Any help would be appreciated.
Thanks!
You can apply all the conditions that you want with a CASE expression:
SELECT * FROM tablename
ORDER BY CASE
WHEN Category = 'Prerequisites' THEN 1
WHEN Category IS NULL THEN 2
ELSE 3
END,
Category,
OrderNum;
or, if there are also empty strings in Category which you want sorted with the NULLs:
SELECT * FROM tablename
ORDER BY CASE
WHEN Category = 'Prerequisites' THEN 1
WHEN COALESCE(Category, '') = '' THEN 2
ELSE 3
END,
Category,
OrderNum;
or:
SELECT * FROM tablename
ORDER BY Category = 'Prerequisites' DESC,
Category IS NULL DESC, -- or: COALESCE(Category, '') = '' DESC,
Category,
OrderNum;
But, if what you want is to sort the rows by the minimum OrderNum of each Category use a correlated subquery:
SELECT t1.* FROM tablename t1
ORDER BY (SELECT MIN(t2.OrderNum) FROM tablename t2 WHERE t2.Category = t1.Category),
t1.OrderNum;
or, for MySql 8.0+ use MIN() window function:
SELECT * FROM tablename
ORDER BY MIN(OrderNum) OVER (PARTITION BY Category),
OrderNum;
See the demo.
If you want the NULL values right after Prerequisites, you can use PrerequisitesZ as the fallback for NULL values:
ORDER BY COALESCE(Category, 'PrerequisitesZ') DESC, OrderNum;
You can try using two conditions in the ORDER BY clause:
if Category has the value "Prerequisites", it should come first
otherwise order by the OrderNum value
Here's how you would do it:
SELECT *
FROM tab
ORDER BY IF(Category='Prerequisites', 0, 1),
OrderNum
Demo here.
Note: Actually you can play how much you want with the conditions in the ORDER BY clause, as it can accept most of MySQL constructs.
I want to fetch last row of the table. For this i have used to below query but it returns me 99 where my table contains approx 123 and last pro_id = 123. Right result should be 123.
please suggest me for right way :
SELECT * FROM product ORDER BY pro_id DESC LIMIT 1
That's most likely because your pro_id column is defined as text.
You can tell sql to order by the numeric value in that column with
SELECT * FROM product ORDER BY CONVERT(pro_id, UNSIGNED INTEGER) DESC LIMIT 1
I think the problem is that your pro_id is character and it will be ordered by alphabetical order. You could try to convert it to number first.
SELECT * FROM product ORDER BY CAST(pro_id AS UNSIGNED) DESC LIMIT 1
This query will display a true desc result:
select pro_id from
(SELECT cast(replace(pro_id,' ','') as UNSIGNED) as pro_id from product) as a
ORDER BY pro_id desc limit 1
suppose there is a table
id desc
1 a
1 b
1 c
1 a
2 a
2 a
what I want to do is to show a mix of desc if the desc under the same id are different, otherswise show the value of the desc. Just like below
id desc
1 a/b/c
2 a
How could I do it in one sql ?
enter code here
I think you want group_concat() with the distinct modifier:
select id, group_concat(distinct `desc` separator '/') as `desc`
from t
group by id;
Note that desc is a bad name for a column because it is a SQL keyword (think order by). It is best to avoid grammatical elements when choosing names.
How can I sort by multiple columns in SQL and in different directions. column1 would be sorted descending, and column2 ascending.
ORDER BY column1 DESC, column2
This sorts everything by column1 (descending) first, and then by column2 (ascending, which is the default) whenever the column1 fields for two or more rows are equal.
The other answers lack a concrete example, so here it goes:
Given the following People table:
FirstName | LastName | YearOfBirth
----------------------------------------
Thomas | Alva Edison | 1847
Benjamin | Franklin | 1706
Thomas | More | 1478
Thomas | Jefferson | 1826
If you execute the query below:
SELECT * FROM People ORDER BY FirstName DESC, YearOfBirth ASC
The result set will look like this:
FirstName | LastName | YearOfBirth
----------------------------------------
Thomas | More | 1478
Thomas | Jefferson | 1826
Thomas | Alva Edison | 1847
Benjamin | Franklin | 1706
SELECT *
FROM mytable
ORDER BY
column1 DESC, column2 ASC
Multiple column ordering depends on both column's corresponding values:
Here is my table example where are two columns named with Alphabets and Numbers and the values in these two columns are asc and desc orders.
Now I perform Order By in these two columns by executing below command:
Now again I insert new values in these two columns, where Alphabet value in ASC order:
and the columns in Example table look like this.
Now again perform the same operation:
You can see the values in the first column are in desc order but second column is not in ASC order.
You can use multiple ordering on multiple condition,
ORDER BY
(CASE
WHEN #AlphabetBy = 2 THEN [Drug Name]
END) ASC,
CASE
WHEN #TopBy = 1 THEN [Rx Count]
WHEN #TopBy = 2 THEN [Cost]
WHEN #TopBy = 3 THEN [Revenue]
END DESC
SELECT id,
first_name,
last_name,
salary
FROM employee
ORDER BY salary DESC, last_name;
If you want to select records from a table but would like to see them sorted according to two columns, you can do so with ORDER BY. This clause comes at the end of your SQL query.
After the ORDER BY keyword, add the name of the column by which you’d like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example, last_name). You can modify the sorting order (ascending or descending) separately for each column. If you want to use ascending (low to high) order, you can use the ASC keyword; this keyword is optional, though, as that is the default order when none is specified. If you want to use descending order, put the DESC keyword after the appropriate column (in the example, we used descending order for the salary column).
SELECT * FROM EMP ORDER BY DEPTNO ASC, JOB DESC;
TRY
'select * FROM users ORDER BY id DESC, name ASC, age DESC
You can also sort or order by the Number of Characters in each Column you wish to sort by. Shown below is a sample which sorts by the first three characters of the First Name and by the last two characters in the name of the town.
SELECT *
FROM table_name
ORDER BY LEFT(FirstName, 3) ASC, LEFT(Town, 2);
I have a problem when use Order By and Group By in a query string.
I want Order By before Group By but it's don't work.
I searched and find some solution but it don't work with me:
SELECT * FROM
(
SELECT minder_id, service_type_id
FROM minder_service
WHERE minder_id = 238
AND deleted_at is null
ORDER BY service_type_id ASC
) AS t
GROUP BY t.minder_id
Run 1
SELECT minder_id, service_type_id
FROM minder_service
WHERE minder_id = 238
AND deleted_at is null
ORDER BY service_type_id ASC
Result:
Photo for Result 1
Run 2: Full
Photo for Result 2
Please have a look at.
Thanks so much.
If you want the lowest service_type_id, you can use the MIN function:
SELECT minder_id, MIN(service_type_id)
FROM minder_service
WHERE minder_id = 238 AND deleted_at IS NULL
GROUP BY minder_id
Also make sure deleted_at is really NULL for the record with service_type_id 1 if you say you expect that record.
A subquery returns an unordered set. The database doesn't have to keep the order when passing the result of a subquery to an outer query, or when performing the group by operation.
You should rethink what you're trying to do.