Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-02-2008, 05:05 AM
Scott Haneda
 
Posts: n/a
Default Age old order by rand() issue

List search seems to return 0 results, and I am a bit stumped.

Getting a more optimized order by random with 1 record...
I found a snip online that works, but seems to return an empty on
occasion, and I am not sure why:

SELECT storage_path, image_md5, id
FROM images
JOIN (SELECT CEIL(RAND() * (SELECT MAX(id) FROM images WHERE
approved = 1)) AS id) AS r2 USING (id)
WHERE approved = 1;

I really do not get this, SELECT CEIL(RAND() will always return 1 will
it not? Any idea why I get an empty result set at times?

I then managed to rig this together:
SELECT * FROM images AS t
JOIN (SELECT CEIL(MAX(id)*RAND()) AS id
FROM images WHERE approved = 1) AS x ON t.id >= x.id LIMIT 1;

This works, but I get an odd result, in that the id column is listed
twice, once at the beginning, where it is in the table, and once at
the end. Duplicate values of course.

If I narrow the * to a real called select, such as
SELECT id, storage_path, image_md5 FROM images AS t
JOIN (SELECT CEIL(MAX(id)*RAND()) AS id
FROM images WHERE approved = 1) AS x ON t.id >= x.id LIMIT 1;

-> FROM images WHERE approved = 1) AS x ON t.id >= x.id LIMIT 1;
ERROR 1052 (23000): Column 'id' in field list is ambiguous

I can not seem to get past that error, and would like to call just the
columns I want. Or, if someone has a better random record return
select, I am all ears.

Thanks so much.

--
Scott
talklists@newgeo.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-02-2008, 05:05 AM
Rob Wultsch
 
Posts: n/a
Default Re: Age old order by rand() issue

> SELECT storage_path, image_md5, id
> FROM images
> JOIN (SELECT CEIL(RAND() * (SELECT MAX(id) FROM images WHERE approved =
> 1)) AS id) AS r2 USING (id)
> WHERE approved = 1;
>
> I really do not get this, SELECT CEIL(RAND() will always return 1 will it
> not? Any idea why I get an empty result set at times?


http://dev.mysql.com/doc/refman/5.0/...#function_rand
So CEIL(RAND() ) would always be 1.
My guess is you have is that you have hole(s) in your data set.

For the record you are not doing CEIL(RAND() ), your doing CEIL(RAND()
* (SELECT MAX(id) FROM images WHERE approved => 1))

--
Rob Wultsch
wultsch@gmail.com
wultsch (aim)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-02-2008, 05:05 AM
Rob Wultsch
 
Posts: n/a
Default Re: Age old order by rand() issue

Hi,
Responses inline....

On Thu, May 1, 2008 at 3:11 PM, Scott Haneda <talklists@newgeo.com> wrote:
> List search seems to return 0 results, and I am a bit stumped.
>
> Getting a more optimized order by random with 1 record...
> I found a snip online that works, but seems to return an empty on occasion,
> and I am not sure why:
>
> SELECT storage_path, image_md5, id
> FROM images
> JOIN (SELECT CEIL(RAND() * (SELECT MAX(id) FROM images WHERE approved =
> 1)) AS id) AS r2 USING (id)
> WHERE approved = 1;
>
> I really do not get this, SELECT CEIL(RAND() will always return 1 will it
> not? Any idea why I get an empty result set at times?


http://dev.mysql.com/doc/refman/5.0/...#function_rand
So CEIL(RAND() ) would always be 1.
My guess is you have is that you have hole(s) in your data set.

>
> I then managed to rig this together:
> SELECT * FROM images AS t
> JOIN (SELECT CEIL(MAX(id)*RAND()) AS id
> FROM images WHERE approved = 1) AS x ON t.id >= x.id LIMIT 1;
>
> This works, but I get an odd result, in that the id column is listed twice,
> once at the beginning, where it is in the table, and once at the end.
> Duplicate values of course.


Using a star is less than a great idea. You are a second id from
CEIL(MAX(id)*RAND()) AS id being joined in.
Specify the columns you want...

> If I narrow the * to a real called select, such as
> SELECT id, storage_path, image_md5 FROM images AS t
> JOIN (SELECT CEIL(MAX(id)*RAND()) AS id
> FROM images WHERE approved = 1) AS x ON t.id >= x.id LIMIT 1;
>
> -> FROM images WHERE approved = 1) AS x ON t.id >= x.id LIMIT 1;
> ERROR 1052 (23000): Column 'id' in field list is ambiguous

Mysql wants you to specify what table you want the id from. Meaning
from x or t...
SELECT t.id, storage_path, image_md5 FROM images AS t
JOIN (SELECT CEIL(MAX(id)*RAND()) AS id
FROM images WHERE approved = 1) AS x ON t.id >= x.id LIMIT 1;

I have seen nicer fast random row implement, but that will work.

--
Rob Wultsch
wultsch@gmail.com
wultsch (aim)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-05-2008, 05:51 AM
Scott Haneda
 
Posts: n/a
Default Re: Age old order by rand() issue

> I have seen nicer fast random row implement, but that will work.


Do you happen to have a snip of it, the one I have seems to lean
pretty heavy as far as I can tell, and on occasion, though rare, also
sends me an empty result set.
--
Scott
talklists@newgeo.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-05-2008, 05:51 AM
Rob Wultsch
 
Posts: n/a
Default Re: Age old order by rand() issue

On Fri, May 2, 2008 at 6:58 AM, Scott Haneda <talklists@newgeo.com> wrote:
>
> > I have seen nicer fast random row implement, but that will work.

>
>
> Do you happen to have a snip of it, the one I have seems to lean pretty
> heavy as far as I can tell, and on occasion, though rare, also sends me an
> empty result set.
> --
> Scott
> talklists@newgeo.com
>


You should not be getting empty results with the second/third query.
The reason you were (probably) previously getting empty results with
the first query was because you were doing the join using USING (aka
=) rather than >= . You were also doing a WHERE clause on that could
have removed the random result.

My only problem with what you are using is that it is more likely to
give a large results than a small one. Take a look at the
http://jan.kneschke.de/projects/mysql/order-by-rand/

You probably do not need this.
If you have a large data set, you probably don't want this.

--
Rob Wultsch
wultsch@gmail.com
wultsch (aim)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:06 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0

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 140 141 142 143 144 145