Skip to: site menu |main content

Welcome to my personal site. If You have any question or suggestion please feel free to contact me from contact page.

Advertisements

Contact me to place your Ads Here..

Powered by Drupal, an open source content management system

Syndicate

Syndicate content

mysql

Mysql Sorting Field function ,Number And character fields Mysql

For sorting in ascending order numbers which are written in text or characters:-
mysql> select number from (table) order by number;
+--------+
| number |
+--------+
| 1 |
| 10 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+--------+

Use this:

Custom sort By ON Fields Mysql

We can use field_functions from mysql for custom sort or Can use CASE, IF,ELSE functionalities in mysql.
i.e:
ORDER BY CASE
WHEN `col` = 'item' THEN 1
WHEN `col` = 'thing' THEN 2
WHEN `col` = 'stuff' THEN 3
WHEN `col` = 'boom' THEN 4
ELSE 5
END

Example:-
1)
SELECT link_path,mlid FROM `rts1_menu_links` order by CASE WHEN link_path="photos/upload" THEN 1 ELSE 5 END,link_path DESC
2)select id from products where id in (4,5,6,7,8,9) order by field(id,4,7,6,5,9,8)

SELECT id, start_date FROM iddt WHERE id IN
('109k7','s3x6','sxmns','wt57')

Syndicate content

Back to top