Jump to content

Recommended Posts

MySQL version: 5.1.54-community

 

I am executing the SQL within PHP MyAdmin 3.3.8.1

 

This will successfully return records (last names beginning with 'B'):

SELECT * FROM `users` WHERE `last_name` LIKE 'B%'

 

However, this query is SUPPOSED to return records of users with last names that begin with letters A-F:

SELECT * FROM `users` WHERE `last_name` LIKE '[A-F]%'

 

Here is the table I am querying:

CREATE TABLE IF NOT EXISTS `users` (
  `userid` varchar(25) NOT NULL DEFAULT '',
  `first_name` varchar(50) NOT NULL DEFAULT '',
  `last_name` varchar(50) NOT NULL DEFAULT '',
  `email` varchar(100) NOT NULL DEFAULT '',
  `work_address1` varchar(100) NOT NULL DEFAULT '',
  `work_address2` varchar(100) DEFAULT '',
  `work_city` varchar(100) NOT NULL DEFAULT '',
  `work_state` char(2) NOT NULL DEFAULT '',
  `work_zip` varchar(10) NOT NULL DEFAULT '',
  `home_address1` varchar(100) DEFAULT '',
  `home_address2` varchar(100) DEFAULT '',
  `home_city` varchar(100) DEFAULT '',
  `home_state` char(2) DEFAULT '',
  `home_zip` varchar(10) DEFAULT '',
  `work_phone` varchar(20) NOT NULL DEFAULT '',
  `home_phone` varchar(20) DEFAULT '',
  `mobile_phone` varchar(20) DEFAULT '',
  `fax` varchar(20) DEFAULT '',
  `primary_phone` varchar(100) NOT NULL DEFAULT '',
  `manager` varchar(100) DEFAULT '',
  `connection_type` varchar(100) DEFAULT '',
  `organization` varchar(255) DEFAULT NULL,
  `group_id` int(10) unsigned DEFAULT '1',
  `must_update` int(2) unsigned NOT NULL DEFAULT '0',
  `must_change_pw` int(1) unsigned NOT NULL DEFAULT '0',
  `last_updated` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `use_cached_login` int(1) unsigned NOT NULL DEFAULT '0',
  `active` int(2) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

My goal is to select users with last names that begin with a range of letters (e.g., A-F). Any help is greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/241748-use-like-wildcard/
Share on other sites

Not sure with this one, the SQL syntax is correct, have you tried debugging your query using mysql_error?

 

Query does not throw an error (i.e., syntax is good), it just won't return any records and there are user names in the table with last names that begin with the range of letters.

Link to comment
https://forums.phpfreaks.com/topic/241748-use-like-wildcard/#findComment-1242211
Share on other sites

You are confusing LIKE with REGEX, LIKE uses a simple wildcard, %. That is it. If you want to be more definitive in your selection, MySQL has a REGEX method. Which would be able to do what you want. If you do not have REGEX in your version of MySQL, you will be stuck with using multiple likes for each letter you want to test against (unless I am missing some unknown function to me). 

Link to comment
https://forums.phpfreaks.com/topic/241748-use-like-wildcard/#findComment-1242269
Share on other sites

You are confusing LIKE with REGEX, LIKE uses a simple wildcard, %. That is it. If you want to be more definitive in your selection, MySQL has a REGEX method. Which would be able to do what you want. If you do not have REGEX in your version of MySQL, you will be stuck with using multiple likes for each letter you want to test against (unless I am missing some unknown function to me).

You actually can use character grouping with SQL, i'm not sure why it is not working in this case.

Link to comment
https://forums.phpfreaks.com/topic/241748-use-like-wildcard/#findComment-1242287
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.