SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Simple MYsql Question

    Hey guys,

    I was wondering if there is a way to check against part of an entry in a database?

    So say I had a column named Name and an entry Derek, Mario, Matt within that column.

    Is there a way to test against part of that entry? For instance if I wanted to pull only the data hat begins with Ma or ends in ek.

    Another Example...

    Quote

    Blah-a
    Blah-b
    Gah-a

    How can I pull only the entries that end in -a?

    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  2. #2
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use LIKE or a REGEX

    // like... match the end of the field / column value

    Code:
    SELECT a, b, c FROM table WHERE name LIKE '%a';
    // regexp... match the end of the field / column value

    Code:
    SELECT a, b, c FROM table WHERE name REGEXP 'a+$';
    You can match more than one. For example...

    // match names ending with... r, s, t

    Code:
    SELECT a, b, c FROM table WHERE name REGEXP '(r|s|t)+$';
    // If using LIKE you have to add an OR with another LIKE

    Code:
    SELECT a, b, c FROM table WHERE name LIKE '%r' OR name LIKE '%s' OR name LIKE '%t';

  3. #3
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the prompt reply. I will try those out right away mysql is making more and more sense everyday now.
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    printf, very nice reply, very comprehensive
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quick followup question, thought it would be better to keep it in the same thread.

    What if I wanted to match against a something that contained AP but only wanted to display the results that start with a B?

    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    use
    Code:
    ... LIKE 'B%AP%'
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, I gave it a shot and it worked, however I realized I was actually trying something else...

    First Name

    Bob
    Mike
    John

    Last Name

    Mo
    Mo
    Bo

    Find all the last names that contain Mo. Then only list the ones that have the first name begin with B's.

    I'm trying to search around for some tutorials on this topic but aren't having much luck.

    Thanks again,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    WHERE Lastname LIKE '%Mo%' AND Firstname Like 'B%'
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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