SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I do a select query

    hey!
    im a newbie at mysql

    im trying to do a select query using "AND" but what i want is to be able to do a query using the same condition more than once, for example

    $query="SELECT * FROM students WHERE address='newyork' AND address='mexico'";

    so as u can see i want to select all students from where address is new york and mexico...can somebody please help!

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    an address cannot simultaneously have two different values

    perhaps you meant OR instead of AND

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OR wont work..because OR will only test for one or the other not for both...
    I tried using or but as long as the first condition is true it wont move on to the second condition.

    for example
    $query="SELECT * FROM students WHERE address='newyork' OR address='mexico'";

    as long as records exist where address=newyork, it will not move to the second condition...

  4. #4
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    When you use AND that way, the same field in a single record has to meet both criteria at the same time, which is not possible. An address cannot be newyork AND mexico simultaneously. So zero records will be returned. Always.

    When you use OR, it will return records where address=newyork, and it will also return records where address=mexico.

    A simpler example.
    If you wished to return all students who were male and all students who were female, you'd use
    where gender=male OR gender = female
    You would not use
    where gender = male AND gender = female.

    I think you might want to give us a set of typical records, and point out the ones you wish to retrieve.

  5. #5
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay i see, i will definately try this again and get back to you...thanks alot, i really appreciate it!

  6. #6
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, OR Definately worked! Thanks a million!


Tags for this Thread

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
  •