SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru redhillccwebmas's Avatar
    Join Date
    Jun 2005
    Posts
    989
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL: Selecting multiple records using WHERE function

    Hi there,

    I want to select multiple records using the WHERE function. Currently my SQL query is as below:-

    strSQL = "SELECT albums.* FROM albums WHERE id = 1"

    As well as selecting id = 1, I would like to select id = 2 and id = 3 .

    How can I write this? I have tried seperating it by commas and spaces but this didn't work.

    Your help is much appreciated

    Kind Regards

    James

  2. #2
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    couple of ways:
    "WHERE id IN (1,2,3)"
    "WHERE id = 1 OR id = 3 OR id = 2..."

    obviously, the IN keyword is easier to write...

  3. #3
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could also do: "WHERE id <= 5"

  4. #4
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    last thing, AND wouldn't work because the ID would have to equal all the numbers you put there 1,2 & 3, which it obviously wouldn't. OR would be what you'd want in your first case.

  5. #5
    SitePoint Guru redhillccwebmas's Avatar
    Join Date
    Jun 2005
    Posts
    989
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers for that

  6. #6
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem. i do need to learn to not hit the "Post" button so quickly though...


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
  •