MySQL-এ শর্ত মেনে একাধিক রো মেললে শুধু প্রথম রোটি পেতে LIMIT 1 ব্যবহার করুন।
সিনট্যাক্স
SELECT * FROM yourTableName WHERE yourCondition LIMIT 1;
উদাহরণ
টেবিল তৈরি করি:
mysql> create table selectWithPrimaryKey
-> (
-> Id int NOT NULL AUTO_INCREMENT,
-> Name varchar(20),
-> Age int,
-> Marks int,
-> PRIMARY KEY(Id)
-> ); Query OK, 0 rows affected (0.78 sec)
রেকর্ড ইনসার্ট:
mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('Larry',24,98);
mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('John',23,89);
mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('Mike',21,85);
mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('Sam',26,56);
mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('Carol',21,59);
mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('Bob',20,91);
mysql> insert into selectWithPrimaryKey(Name,Age,Marks) values('David',28,93);
সকল রেকর্ড
mysql> select *from selectWithPrimaryKey;
আউটপুট
+----+-------+------+-------+ | Id | Name | Age | Marks | +----+-------+------+-------+ | 1 | Larry | 24 | 98 | | 2 | John | 23 | 89 | | 3 | Mike | 21 | 85 | | 4 | Sam | 26 | 56 | | 5 | Carol | 21 | 59 | | 6 | Bob | 20 | 91 | | 7 | David | 28 | 93 | +----+-------+------+-------+
শর্ত মেনে প্রথম রো (Id=10 নেই, Age=29 নেই, Marks=89 হ্যাঁ → John)
mysql> SELECT * FROM selectWithPrimaryKey WHERE Id = 10 OR Age = 29 OR Marks = 89 LIMIT 1;
ফলাফল
+----+------+------+-------+ | Id | Name | Age | Marks | +----+------+------+-------+ | 2 | John | 23 | 89 | +----+------+------+-------+ 1 row in set (0.00 sec)
নোট: LIMIT 1 বিনা ORDER BY থাকলে ফলাফল অনির্দিষ্ট হতে পারে। নির্দিষ্ট রো চাইলে ORDER BY যোগ করুন।