lukep11a Posted January 23, 2011 Share Posted January 23, 2011 Hi, I currently have this code that selects all names from the table that begin with the letter 'A', does anybody know how to select all entries from the table that begin with a number? $query = "SELECT * FROM table WHERE name LIKE 'A%' Any help would be very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/225433-select-all-entry-beggining-with-any-number/ Share on other sites More sharing options...
harristweed Posted January 23, 2011 Share Posted January 23, 2011 Well I guess: $query = "SELECT * FROM table WHERE name LIKE '1%' OR name LIKE '2%' name LIKE '3%' name LIKE '4%'................." will do it! Quote Link to comment https://forums.phpfreaks.com/topic/225433-select-all-entry-beggining-with-any-number/#findComment-1164121 Share on other sites More sharing options...
lukep11a Posted January 23, 2011 Author Share Posted January 23, 2011 Thanks mate... that worked! Quote Link to comment https://forums.phpfreaks.com/topic/225433-select-all-entry-beggining-with-any-number/#findComment-1164124 Share on other sites More sharing options...
fortnox007 Posted January 24, 2011 Share Posted January 24, 2011 I am not sure if this works, but I would rather use some regex instead of repeating 10 likes in 1 query. Maybe try this out: $query = "SELECT * FROM table WHERE name REGEXP '^[0-9]'"; for some more insight check this out : http://dev.mysql.com/doc/refman/5.0/en/regexp.html Quote Link to comment https://forums.phpfreaks.com/topic/225433-select-all-entry-beggining-with-any-number/#findComment-1164231 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2011 Share Posted January 24, 2011 SELECT * FROM table WHERE name REGEXP '^[0-9]' isn't a valid SQL statement. Quote Link to comment https://forums.phpfreaks.com/topic/225433-select-all-entry-beggining-with-any-number/#findComment-1164239 Share on other sites More sharing options...
Psycho Posted January 24, 2011 Share Posted January 24, 2011 SELECT * FROM table WHERE name REGEXP '^[0-9]' isn't a valid SQL statement. Sure it is. If you received an error you might want to check which version of MySQL you are using. REGEXP was apparently introduced with ver. 5.1. Of course this assumes that you don't literally have a table named "table". If you do,then you need to enclose the table name in backquotes. SELECT * FROM `table` WHERE name REGEXP '^[0-9]' Quote Link to comment https://forums.phpfreaks.com/topic/225433-select-all-entry-beggining-with-any-number/#findComment-1164244 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2011 Share Posted January 24, 2011 Just looked again and the reason it returned an error was the closing single quote around the pattern was truncated, so you're right. It was my fault for a bad copy/paste it seems. Quote Link to comment https://forums.phpfreaks.com/topic/225433-select-all-entry-beggining-with-any-number/#findComment-1164247 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.