If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Mysql wildcards support?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-03-08, 02:20
sjgrad03 sjgrad03 is offline
Registered User
 
Join Date: Aug 2003
Location: san jose, CA
Posts: 68
Mysql wildcards support?

hello everyone i am wondering if Mysql supports
range operator and negation character in wildcards.

In my talbe i have a column called lastname:
records in lastname are: hansen, svendson, petterson and peter.

1. I wrote a query try to find out last name start with letter 'h', 's' or 'p'
followed by any characters.

Code:
  select * from person 
  where lastname like '[hsp]%';
the result retured an empty set. Seems to me Mysql doesn't support range
operator []

2.
Code:
    select * from person
    where firstname like '[^t]%';
I want to find out that entry that doesn't start with letter 't'
the result also returned a empty set.

Please give me some advises on how to make wildcards works in these 2 cases.
Reply With Quote
  #2 (permalink)  
Old 11-03-08, 02:39
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by sjgrad03
hello everyone i am wondering if Mysql supports
range operator and negation character in wildcards.
You are trying to use regular expressions here. The syntax for that is different (because it's not standard SQL). See the manual:
http://dev.mysql.com/doc/refman/5.0/en/regexp.html

Quote:
I want to find out that entry that doesn't start with letter 't' the result also returned a empty set.
Code:
WHERE firstname NOT LIKE 't%'
Reply With Quote
  #3 (permalink)  
Old 11-03-08, 03:51
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
and this is how it's done using the regular expressions but it's better to do it using like though as this is easier to read:

Code:
select * from person 
where lastname rlike '^[hsp]';
Code:
select * from person
where firstname rlike '^[^t]';
Note :
  • the ^ starts the comparison at the beginning of the line.
  • the ^ after the [ means do not match against these characters.
  • the % doesn't have any special meanings with rlike.
  • you could put .* instead of % which means the same thing for rlike but it's not necessary.
  • the keyword regexp does the same as rlike.

Mike
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On