MySQL কোয়েরির ফলাফলে সিরিয়াল নম্বর বা রো কাউন্ট যোগ করতে ইউজার-ডিফাইনড ভেরিয়েবল ব্যবহার করুন।
সিনট্যাক্স
SELECT @yourVariableName := @yourVariableName+1 AS anyAliasName, yourColumnName1, yourColumnName2, ... FROM yourTableName, (SELECT @yourVariableName := 0) AS yourVariableName;
উদাহরণ
টেবিল তৈরি করি:
mysql> create table tblStudentInformation
-> (
-> StudentName varchar(20),
-> StudentAge int,
-> StudentMathMarks int
-> ); Query OK, 0 rows affected (0.68 sec)
রেকর্ড যোগ করি:
mysql> insert into tblStudentInformation values('Carol',23,89);
mysql> insert into tblStudentInformation values('Bob',25,92);
mysql> insert into tblStudentInformation values('John',21,82);
mysql> insert into tblStudentInformation values('David',26,98);
সকল রেকর্ড
mysql> select *from tblStudentInformation;
আউটপুট
+-------------+------------+------------------+ | StudentName | StudentAge | StudentMathMarks | +-------------+------------+------------------+ | Carol | 23 | 89 | | Bob | 25 | 92 | | John | 21 | 82 | | David | 26 | 98 | +-------------+------------+------------------+ 4 rows in set (0.00 sec)
সিরিয়াল নম্বর যোগ করুন
mysql> SELECT @serialNumber := @serialNumber+1 AS yourSerialNumber, StudentName, StudentAge, StudentMathMarks FROM tblStudentInformation, (SELECT @serialNumber := 0) AS serialNumber;
ফলাফল
+------------------+-------------+------------+------------------+ | yourSerialNumber | StudentName | StudentAge | StudentMathMarks | +------------------+-------------+------------+------------------+ | 1 | Carol | 23 | 89 | | 2 | Bob | 25 | 92 | | 3 | John | 21 | 82 | | 4 | David | 26 | 98 | +------------------+-------------+------------+------------------+ 4 rows in set (0.00 sec)
আধুনিক বিকল্প: MySQL 8.0+ এ ROW_NUMBER() OVER () উইন্ডো ফাংশন ব্যবহার করা সহজ ও সুপারিশ করা হয়।