SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql query : selecting specific records

    What is the proper way to select specific records from a database?

    For example I want to select the records with the following id : 5,11 and 19.

    What I do now is this :

    SELECT * FROM table WHERE id=5 OR id=11 OR id=19

    There must be another way to do this though

  2. #2
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unless records 5, 11 and 19 have something in common there isn't.
    Why do you want those particular records? You can use < = <= like etc. to pick out which records you want.
    arelgee

  3. #3
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well it's for the following:

    I have a list of products.
    Visitors can select products to order, say they select the products with id 5,11 and 19.

    Then to show the products they have ordered I need to retrieve the info from the database.
    So I need to retrieve records 5,11 and 19.

    I know I could store the selected products in a seperate table, but I don't need that information to be kept in a database.

    Now that I think of it,I could also store the title and price in the session variable, so I would not have to query the database again....so many options

  4. #4
    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)
    if the users are using checkboxes, you will typically get the checked values in an array

    just convert the array into a list of numbers, and stuff the entire list into the query

    the structure you want is called an IN list

    ... WHERE id IN ( 5, 11, 19 )


    rudy

  5. #5
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by r937
    the structure you want is called an IN list

    ... WHERE id IN ( 5, 11, 19 )


    rudy
    That's what I was looking for! Thanks


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
  •