Mysql - how do I get a list starting with a letter and continue with the following letters?

SELECT contact_id, last_name FROM contacts
WHERE last_name like 'B%' ORDER BY last_name limit 0, 250

returns only the letters B.

I need to return 250 rows starting from the first Bs. If there are less than 250 Bs, I need to get the following Cs, Ds, etc.

+3
source share
4 answers

Caveat: This comparison works on SQL Server, I also assume that it works with MySQL, but cannot test it.

SELECT contact_id, last_name 
FROM contacts
WHERE last_name > 'B' 
ORDER BY last_name 
LIMIT 0, 250

Will exclude entries read as alphanumeric β€œless”.

+7
source

Most DB engines compare text in such a way that it will work ...

SELECT contact_id, last_name FROM contacts
WHERE last_name > 'B' ORDER BY last_name limit 0, 250
+1
source

you can use OR your operators

SELECT contact_id, last_name FROM contacts
WHERE last_name like 'B%' or last_name like 'C%' or last_name like 'D%' ORDER BY last_name limit 0, 250

etc.

0
source

What about "A%"? (so you get As and Bs will be the first) or Regexp?

0
source

Source: https://habr.com/ru/post/1789335/


All Articles