patternsqlMinor
search for something in the first 1000 records in the table
Viewed 0 times
thesearchtablerecordsfirstfor1000something
Problem
If I do the following MySQL query
that mean the query will search all the mysql database table and return only the top 5 results that match the query where condition.
BUT, what if want to just search the top 1000 records in the database table that match the where condition and return only the first 5 rows , because I have 1,000,000 records in the table and i just want to search in the first 1000 records, what should i use?
this is the wanted query in human readable:
search the top 1000 records from the table which have the name like john and return only the top 5 records that match.
what is the corresponding in MySQL
select * from table where name Like '%john%' limit 5;that mean the query will search all the mysql database table and return only the top 5 results that match the query where condition.
BUT, what if want to just search the top 1000 records in the database table that match the where condition and return only the first 5 rows , because I have 1,000,000 records in the table and i just want to search in the first 1000 records, what should i use?
this is the wanted query in human readable:
search the top 1000 records from the table which have the name like john and return only the top 5 records that match.
what is the corresponding in MySQL
Solution
You can't do something like a 'first 1000' without an order, since each query could give you a different order if you don't specify it.
So, let's try this way:
So, let's try this way:
select * from (select * from table order by PUTYOURORDERFIELDHERE LIMIT 1000) where name
like '%john%' limit 5;Code Snippets
select * from (select * from table order by PUTYOURORDERFIELDHERE LIMIT 1000) where name
like '%john%' limit 5;Context
StackExchange Database Administrators Q#40221, answer score: 5
Revisions (0)
No revisions yet.