Using MySQL ORDER BY Clause you can sort table columns by ascending or descending.
Suppose we have a table ‘tbl’ with column ‘name’ and a row like below values:
| 1 |
| 1B |
| 10 |
| 2 |
| 10C |
| 2A |
| 10Z |
SELECT name FROM tbl ORDER BY name;
1
10
10C
10Z
1B
2
2A
This is not like that what we expected. This ia natural sorting.
So we need to CAST this to get our desired result like below:
SELECT name FROM tbl ORDER BY CAST(name AS UNSIGNED);
Result:
1
1B
2
2A
10
10C
10Z
ORDER BY FIELD:
Sometimes a specific order is required in a query which cannot be done using either ASC or DESC or using a special sort field.
Using FIELD( ) function in the ORDER BY clause you can achieve this.
SELECT name FROM tbl ORDER BY FIELD(name, '1B', '2A') desc;
Result:
2A
1B
1
10
2
10C
10Z
Finally, your experiences will develop you as a leader, and the specific abilities you'll need…
Assess Your Primary Use Case First question: what are you actually doing with a VPN—streaming,…
Data is an essential aspect of today's business. Moreover, most companies gather customer data at…
More and more countries go back to their unique national identity and rediscover long-forgotten foods,…
In today’s fast-paced and highly competitive marketing environment, even the most creative campaign is only…
We are NYC moving firm. Are you planning a flat move? Maybe a distance or…