Grabeorama Posted March 10, 2010 Share Posted March 10, 2010 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. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2010 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 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 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 Share Posted March 11, 2010 Okay thanks I'll try that now.EDIT: Thanks that works Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.