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

Time:02-11

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'.

CodePudding user response:

If you only want the ID's you need:

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

CodePudding user response:

Since these are strings, try this:

SELECT id
FROM tableName
WHERE nameField >= 'Landon' and nameField <= 'Peter'
ORDER BY nameField;

If you want to use the full name (first and last), you can try with that too.

  • Related