#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    18
    Rep Power
    0

    Using Wildcard in MYSQL


    Hi! I want to select persons with a last name that starts with "b" or "s" or "p" from the "Persons" table. Here goes SELECT Statement,

    SELECT * FROM Persons WHERE LastName LIKE '[bsp]%'

    But MySQL returned an empty result set. How to fix it?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2007
    Location
    Purley, Surrey
    Posts
    248
    Rep Power
    36
    Originally Posted by RajaGa
    Hi! I want to select persons with a last name that starts with "b" or "s" or "p" from the "Persons" table. Here goes SELECT Statement,

    SELECT * FROM Persons WHERE LastName LIKE '[bsp]%'

    But MySQL returned an empty result set. How to fix it?
    SELECT * FROM Persons WHERE left(LastName,1) in ('b','s','p')
  4. #3
  5. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,204
    Rep Power
    4279
    Originally Posted by RajaGa
    WHERE LastName LIKE '[bsp]%'
    that's ms sql server syntax, and it doesn't work in mysql

    use tony's solution, or learn mysql's REGEX syntax
    Code:
    WHERE LastName REGEX '^[bsp]'
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    18
    Rep Power
    0
    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo