SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to select multi rows from one table with id?

    I want to select two rows from a table, I wrote a query but it's not work. it's seem wrong. How should I repair?

    Here is code:
    PHP Code:
    mysql_query("SELECT * FROM product WHERE id = 2 AND id = 5"); 

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try this code:
    Code:
    mysql_query("SELECT * FROM product WHERE id IN(2, 5) LIMIT 2");
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MarPlo View Post
    Hi,
    Try this code:
    Code:
    mysql_query("SELECT * FROM product WHERE id IN(2, 5) LIMIT 2");
    Great! Thanks.

  4. #4
    SitePoint Member imsas's Avatar
    Join Date
    Feb 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also try an "OR".. For example:
    mysql_query("SELECT * FROM product WHERE id=2 OR id=5");
    ___________________________________________________________________
    IMSAS Background Check - Background Check API
    PeopleSearchWorld.com - People Search
    fuzwire.com - Job Search

  5. #5
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imsas View Post
    You can also try an "OR".. For example:
    Thanks but I want to display all them.

  6. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    IN does the same as a group of ORs, looking around it appears that IN runs faster then a group of ORs. For a small site or one with not too many records the difference in speed probably won't be worth worrying about but for a big site you'll want to go with IN. Another advantages of IN is that it's a little more readable and saves some typing. I personally would go for IN, not so much for the speed of execution but more for saving on typing and it's more readable.l
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  7. #7
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    IN does the same as a group of ORs, looking around it appears that IN runs faster then a group of ORs. For a small site or one with not too many records the difference in speed probably won't be worth worrying about but for a big site you'll want to go with IN. Another advantages of IN is that it's a little more readable and saves some typing. I personally would go for IN, not so much for the speed of execution but more for saving on typing and it's more readable.l
    Oh! I see. It's very useful. Thank you and sorry imsas!


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
  •