Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2003
    Location
    san jose, CA
    Posts
    68

    Unanswered: 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.

  2. #2
    Join Date
    Nov 2003
    Posts
    2,935
    Provided Answers: 12
    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

    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%'

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •