MySQL Query Help

Discussion in 'MySQL' started by eddiemoth, Mar 26, 2009.

  1. #1
    Hi folks,
    I need a little help here to get my little MySQL query to work.

    SELECT user_id, username, user_posts, user_avatar FROM cms_users WHERE username != 'X' ORDER BY user_posts DESC LIMIT 0,10

    In the WHERE clause if I want to get the same result but not equal X, Y, and Z. More than just X. How do I do that?
     
    eddiemoth, Mar 26, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    SELECT user_id, username, user_posts, user_avatar
    FROM cms_users
    WHERE username NOT IN ('X','Y','Z')
    ORDER BY user_posts DESC LIMIT 0,10
     
    jestep, Mar 27, 2009 IP
  3. eddiemoth

    eddiemoth Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the hint. Now I take that concept and applied it in my php code below.

    Original code

    
    
    $result=$db->sql_query('SELECT a.user_id, a.username, a.user_posts, a.user_avatar FROM '.$user_prefix.'_users a ORDER BY user_posts DESC LIMIT 0,14');
    
    
    Code (markup):
    I added NOT IN ('Admin', 'Helitown')
    Because I don't want to get the users Admin and Helitown and I get errorunexpected T_STRING. please see the modified code below.


    
    $result=$db->sql_query('SELECT a.user_id, a.username, a.user_posts, a.user_avatar FROM '.$user_prefix.'_users a WHER a.username NOT IN ('Admin', 'helitown')ORDER BY user_posts DESC LIMIT 0,7');
    
    Code (markup):
    Can someone help me to avoid the error please?
    Thanks a lot
     
    eddiemoth, Mar 27, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Try this:

    $result=$db->sql_query('SELECT a.user_id, a.username, a.user_posts, a.user_avatar FROM '.$user_prefix.'_users a WHER a.username NOT IN (\'Admin\', \'helitown\')ORDER BY user_posts DESC LIMIT 0,7');

    or:

    $result=$db->sql_query("SELECT a.user_id, a.username, a.user_posts, a.user_avatar FROM ".$user_prefix."_users a WHER a.username NOT IN ('Admin', 'helitown')ORDER BY user_posts DESC LIMIT 0,7");

    It's because the ' needs to be \' or use " instead.
     
    jestep, Mar 27, 2009 IP
  5. eddiemoth

    eddiemoth Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I tried both ways and I got - so it is no longer a expected string problem

    A database error has occurred

    The webmaster has been notified of the error
     
    eddiemoth, Mar 27, 2009 IP
  6. Siteapps

    Siteapps Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    SELECT user_id, username, user_posts, user_avatar FROM cms_users WHERE username NOT IN ('X','Y','Z') ORDER BY user_posts DESC LIMIT 0,10
     
    Siteapps, Mar 28, 2009 IP
  7. eddiemoth

    eddiemoth Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi jestep,
    Actually, I found the problem. It is working now after I corrected the spelling of WHER to WHERE. :) Thanks a lot.



     
    eddiemoth, Mar 28, 2009 IP