Grabeorama Posted March 10, 2010 Report Share Posted March 10, 2010 (edited) Is there a way of retrieving data from a table, but only sorting by it's first letter?For example if I wanted to pull all entries for the column 'Names' that started with the letter A, then on a separate occasion sort them by the letter B etc. Edited March 13, 2010 by Grabeorama Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2010 Report Share Posted March 10, 2010 You can use any of the string functions in MySQL to compare or transform the fields.http://dev.mysql.com/doc/refman/5.1/en/string-functions.html Link to comment Share on other sites More sharing options...
Grabeorama Posted March 10, 2010 Author Report Share Posted March 10, 2010 So would it be something like this:$sql = "SELECT * FROM blog WHERE Name = `SUBSTR('A',1)`";mysql_query($sql); Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2010 Report Share Posted March 11, 2010 SELECT * FROM blog WHERE SUBSTR(Name, 1) = 'A'It would probably be better to use the LEFT function though, that returns a certain number of characters from the left side. That sounds like exactly what you need, so the database will probably run that faster. Link to comment Share on other sites More sharing options...
Grabeorama Posted March 11, 2010 Author Report Share Posted March 11, 2010 (edited) Okay thanks I'll try that now.EDIT: Thanks that works Edited March 13, 2010 by Grabeorama Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now