简体   繁体   中英

In mysql how do I only search through the first 10 records

I am writing a query, but I only want to search the first 10 records in the table. I know that in a select limit usually limits the records, but it doesn't work for this instance. eg

SELECT * FROM `logon` WHERE `username`='superman' ORDER BY `user_id` LIMIT 10

The above line will never work because the query only returns one.

I only want to search through the first 10 records, and limit doesn't work in this case.

So how do I limit my search to the first 10 records?

SELECT * FROM
(SELECT * FROM `logon` ORDER BY `user_id` LIMIT 10) as temp
WHERE temp.`username`='superman';

使用顺序

SELECT * FROM `logon` WHERE `username` = 'superman' ORDER BY user_id LIMIT 10 

您可以简单地编写一个子查询,该查询返回“前” 10条记录,并在结果集上放置where子句。

SELECT * 
FROM `logon` 
WHERE `username`='superman' 
and user_id in (select user_id from logon order by user_id limit 10)

(I haven't tried this, but I think it's the fastest way to do this)

Not the right Way...but can be done...

SELECT * FROM logon WHERE username ='superman' AND srno BETWEEN 1 AND 10;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM