Professional Web Applications Themes

Simplier way of doing such SELECT - MySQL

Hey! I currently do such SELECT query to pick some randomly chosen items from database and I can't use ORDER BY RAND() These numbers are selected earlier SELECT DISTINCT id, name, description, price FROM items WHERE id='102323' OR id='13872635' OR id='108253' OR id='105938' OR id='13200092' OR id='13550328' OR id='11746691' OR id='102834' OR id='105427' OR id='106877' OR id='108375' OR id='105884' OR id='11697534' OR id='13410011' OR id='106650' OR id='105138' OR id='106969' OR id='13216002' Is there an easier way of doing this?? Regards...

  1. #1

    Default Simplier way of doing such SELECT

    Hey!
    I currently do such SELECT query to pick some randomly chosen items from
    database and I can't use ORDER BY RAND()
    These numbers are selected earlier

    SELECT DISTINCT id, name, description, price FROM items WHERE id='102323'
    OR id='13872635' OR id='108253' OR id='105938' OR id='13200092' OR
    id='13550328' OR id='11746691' OR id='102834' OR id='105427' OR
    id='106877' OR id='108375' OR id='105884' OR id='11697534' OR
    id='13410011' OR id='106650' OR id='105138' OR id='106969' OR id='13216002'

    Is there an easier way of doing this??

    Regards
    Tomek Guest

  2. #2

    Default Re: Simplier way of doing such SELECT

    Tomek wrote:
    > Hey!
    > I currently do such SELECT query to pick some randomly chosen items from
    > database and I can't use ORDER BY RAND()
    > These numbers are selected earlier
    >
    > SELECT DISTINCT id, name, description, price FROM items WHERE
    > id='102323' OR id='13872635' OR id='108253' OR id='105938' OR
    > id='13200092' OR id='13550328' OR id='11746691' OR id='102834' OR
    > id='105427' OR id='106877' OR id='108375' OR id='105884' OR
    > id='11697534' OR id='13410011' OR id='106650' OR id='105138' OR
    > id='106969' OR id='13216002'
    >
    > Is there an easier way of doing this??
    >
    > Regards
    Use IN instead of OR

    SELECT DISTINCT id, name, description, price
    FROM items
    WHERE
    id IN (102323, 13872635, 108253, 105938, 13200092, 13550328, 11746691,
    102834, 105427, 106877, 108375, 105884, 11697534, 13410011, 106650,
    105138, 106969, 13216002)

    Easier to read, and more efficient too.

    ciao
    gmax

    --
    _ _ _ _
    (_|| | |(_|>< The Data Charmer
    _|
    [url]http://datacharmer.blogspot.com/[/url]
    Giuseppe Maxia Guest

  3. #3

    Default Re: Simplier way of doing such SELECT

    On Wed, 12 Apr 2006 13:19:40 +0200, Giuseppe Maxia <gmax__cpan_._org>
    wrote:
    > Use IN instead of OR
    >
    > SELECT DISTINCT id, name, description, price
    > FROM items
    > WHERE
    > id IN (102323, 13872635, 108253, 105938, 13200092, 13550328,
    > 11746691,
    > 102834, 105427, 106877, 108375, 105884, 11697534, 13410011,
    > 106650,
    > 105138, 106969, 13216002)
    >
    > Easier to read, and more efficient too.
    >
    > \

    Ok thanks, I wonder how didn't i know this structure. Nevermind, thanks
    again

    Tomasz Kraus
    Tomek Guest

Similar Threads

  1. Select a list of items into an aliased field when doinga select
    By ehaemmerle in forum Coldfusion Database Access
    Replies: 3
    Last Post: March 18th, 10:49 PM
  2. Replies: 0
    Last Post: September 24th, 03:24 AM
  3. Replies: 0
    Last Post: September 11th, 11:26 AM
  4. Replies: 0
    Last Post: September 11th, 12:19 AM
  5. Replies: 0
    Last Post: April 15th, 01:22 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139