কম্পিউটার টিউটোরিয়াল

MySQL-এ RIGHT() ফাংশন দিয়ে শেষ ৩ ক্যারেক্টার অনুযায়ী সাজানো

MySQL-এ কোনো স্ট্রিং কলামের শেষ ৩ ক্যারেক্টার অনুযায়ী ফলাফল সাজাতে ORDER BY RIGHT(column, 3) ব্যবহার করুন।

সিনট্যাক্স

SELECT * FROM yourTableName ORDER BY RIGHT(yourColumnName, 3) ASC|DESC;

উদাহরণ

টেবিল তৈরি করি:

mysql> create table OrderByLast3Chars 
    -> ( 
    -> EmployeeId int NOT NULL AUTO_INCREMENT, 
    -> EmployeeName varchar(20), 
    -> EmployeeAge int, 
    -> PRIMARY KEY(EmployeeId) 
    -> ); Query OK, 0 rows affected (0.56 sec)

রেকর্ড যোগ করি (শেষ ৩ অঙ্ক আলাদা):

mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('Carol_901',24); 
mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('Bob_101',21); 
mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('Sam_107',22); 
mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('Mile_677',26); 
mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('John_978',27); 
mysql> insert into OrderByLast3Chars(EmployeeName,EmployeeAge) values('David_876',29);

সকল রেকর্ড

mysql> select *from OrderByLast3Chars;

আউটপুট

+------------+--------------+-------------+
| EmployeeId | EmployeeName | EmployeeAge |
+------------+--------------+-------------+
| 1          | Carol_901    | 24          |
| 2          | Bob_101      | 21          |
| 3          | Sam_107      | 22          |
| 4          | Mile_677     | 26          |
| 5          | John_978     | 27          |
| 6          | David_876    | 29          |
+------------+--------------+-------------+

কেস ১: আরোহী ক্রম (ASC)

mysql> select *from OrderByLast3Chars ORDER BY RIGHT(EmployeeName, 3) ASC;

ফলাফল

+------------+--------------+-------------+
| EmployeeId | EmployeeName | EmployeeAge |
+------------+--------------+-------------+
| 1          | Carol_901    | 24          |
| 2          | Bob_101      | 21          |
| 3          | Sam_107      | 22          |
| 4          | Mile_677     | 26          |
| 5          | John_978     | 27          |
| 6          | David_876    | 29          |
+------------+--------------+-------------+

কেস ২: অবরোহী ক্রম (DESC)

mysql> select *from OrderByLast3Chars ORDER BY RIGHT(EmployeeName, 3) DESC;

ফলাফল

+------------+--------------+-------------+
| EmployeeId | EmployeeName | EmployeeAge |
+------------+--------------+-------------+
| 5          | John_978     | 27          |
| 1          | Carol_901    | 24          |
| 6          | David_876    | 29          |
| 4          | Mile_677     | 26          |
| 3          | Sam_107      | 22          |
| 2          | Bob_101      | 21          |
+------------+--------------+-------------+
  1. MySQL-এ অক্ষরের সংখ্যা অনুযায়ী সাজানো (ORDER BY LENGTH)

  2. MySQL-এ DISTINCT এবং ORDER BY MAX() দিয়ে আইডি অনুযায়ী সাজানো

  3. MySQL-এ NULL মানসহ কলাম সর্ট করা

  4. MySQL কোয়ারি: একাধিক ID দ্বারা সাজানো (ORDER BY FIELD)