1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Multiple [MySQL] Where

Discussion in 'PHP' started by bsdus, Mar 29, 2012.

  1. #1
    I need to using multiple mysql where: Example
    mysql_query("SELECT FROM USER where username='%John%' AND username='%Other%'");
    PHP:
    How to
     
    bsdus, Mar 29, 2012 IP
  2. stasiek

    stasiek Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Hi,
    think, how 1 record can have two different username values? Try two seperate queries:
    
    [URL="http://www.php.net/mysql_query"][COLOR=#990000]mysql_query[/COLOR][/URL][LEFT][COLOR=#009900][FONT=monospace]([/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]"SELECT * FROM user [/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]WHERE[/FONT][/COLOR][COLOR=#0000FF][FONT=monospace] username='%John%'"[/FONT][/COLOR][COLOR=#009900][FONT=monospace])[/FONT][/COLOR][COLOR=#339933][FONT=monospace];[/FONT][/COLOR][/LEFT]
    
    Code (markup):
    
    [URL="http://www.php.net/mysql_query"][COLOR=#990000]mysql_query[/COLOR][/URL][LEFT][COLOR=#009900][FONT=monospace]([/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]"SELECT * FROM user [/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]WHERE[/FONT][/COLOR][COLOR=#0000FF][FONT=monospace] username='%Other%'"[/FONT][/COLOR][COLOR=#009900][FONT=monospace])[/FONT][/COLOR][COLOR=#339933][FONT=monospace];[/FONT][/COLOR][/LEFT]
    
    Code (markup):
    another problem is '*' Look this:
    
    [URL="http://www.php.net/mysql_query"][COLOR=#990000]mysql_query[/COLOR][/URL][LEFT][COLOR=#009900][FONT=monospace]([/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]"SELECT * FROM user WHERE username='%John%'"[/FONT][/COLOR][COLOR=#009900][FONT=monospace])[/FONT][/COLOR][COLOR=#339933][FONT=monospace];[/FONT][/COLOR][/LEFT]
    
    Code (markup):
    That will select all the fields from mysql table. If you want to select only specific fields instead of '*' write something else for example:
    
    [URL="http://www.php.net/mysql_query"][COLOR=#990000]mysql_query[/COLOR][/URL][LEFT][COLOR=#009900][FONT=monospace]([/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]"SELECT username FROM user [/FONT][/COLOR][COLOR=#0000FF][FONT=monospace]WHERE[/FONT][/COLOR][COLOR=#0000FF][FONT=monospace] username='%John%'"[/FONT][/COLOR][COLOR=#009900][FONT=monospace])[/FONT][/COLOR][COLOR=#339933][FONT=monospace];[/FONT][/COLOR][/LEFT]
    
    Code (markup):
    another examples can be found here:
    http://www.tuxradar.com/practicalphp/9/3/13

    best regards,
    s.
     
    stasiek, Mar 29, 2012 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    Do you mean to use LIKE instead of equals?

    Either way, use or instead of and. If that doesn't get what you're after, perhaps try explaining yourself a little better.
     
    Alex Roxon, Mar 29, 2012 IP
  4. trecords

    trecords Well-Known Member

    Messages:
    145
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #4
    You can use WHERE only once but with several ANDs
    Also if you want to join other query LEFT JOIN or RIGHT JOIN may help you
     
    trecords, Mar 30, 2012 IP
  5. zero_ZX

    zero_ZX Member

    Messages:
    83
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #5
    mysql_query("SELECT * FROM USER where username LIKE 'John' AND LIKE 'Other'");
    Selects all rows in user, where the username is *john* and *other*

    mysql_query("SELECT * FROM USER where username LIKE 'John' OR LIKE 'Other'");
    Selects all rows in user, where the username is *john* or *other*
     
    zero_ZX, Mar 30, 2012 IP
    stasiek likes this.
  6. abyssal

    abyssal Guest

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    mysql_query("SELECT * FROM USER where username LIKE '%John%' AND username LIKE '%Other%'");
     
    abyssal, Apr 1, 2012 IP
  7. JellyBean Fanatic

    JellyBean Fanatic Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    MySQL has full-text searching so ideally you'd want to use
    LIKE 'John%'
    PHP:
    . Avoid using wildcard at the start of the search string otherwise it cant use the index for this field. Indexes get pretty large an inefficient for full-text though, so ignore this if you aren't indexing. Although you should if this is just a varchar username field.
     
    JellyBean Fanatic, Apr 1, 2012 IP
  8. Artuurs

    Artuurs Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This multiple select Rowss
     
    Artuurs, Apr 7, 2012 IP