Search code examples
mysqlselectalphabetical

Mysql: Select all values alphabetically between two strings in a table


Okay let's say I have this in my table:

id name
1 tom
2 anna
3 beatrice
4 robert
5 xavier
6 zoe
7 eustace

How can I select all ids of the names that are alphabetically sorted?

Say, select * from myTable where name "between" 'beatrice' and 'tom' order by name;

Should give me :

id name
3 beatrice
7 eustace
4 robert
1 tom

Because in alphabetical order, those are in between 'beatrice' and 'tom'.


Solution

  • If you only want the ID's you need:

    select id from myTable where name between 'beatrice' and 'tom' order by name;