Order by rand()

This code returns 34 rows:

SELECT plan.*, min(trkg.curwipprice_online) AS curwipprice_online, trkg.addr
		FROM trkg
		RIGHT OUTER JOIN plan
		ON plan.cliorder = trkg.cliorder
		WHERE (TIMESTAMP(NOW()) BETWEEN TIMESTAMP(plan.begin) AND TIMESTAMP(plan.end) )
		GROUP BY plan.item_name DESC 

All I changed was GROUP BY… to ORDER BY RAND().

SELECT plan.*, min(trkg.curwipprice_online) AS curwipprice_online, trkg.addr
		FROM trkg
		RIGHT OUTER JOIN plan
		ON plan.cliorder = trkg.cliorder
		WHERE (TIMESTAMP(NOW()) BETWEEN TIMESTAMP(plan.begin) AND TIMESTAMP(plan.end) )
		ORDER BY RAND()

Now only 1 row is selected.

Why?

why? because you dropped the GROUP BY clause

duh duh duh duhhhhhhh. Thanks again as usual. I value your help very much.

I was proceeding from a memory that I couldn’t combine a GROUP BY and an ORDER BY as in

SELECT plan.*, min(trkg.curwipprice_online) AS curwipprice_online, trkg.addr
		FROM trkg
		RIGHT OUTER JOIN plan
		ON plan.cliorder = trkg.cliorder
		WHERE (TIMESTAMP(NOW()) BETWEEN TIMESTAMP(plan.begin) AND TIMESTAMP(plan.end) )
		GROUP BY plan.item_name DESC
		ORDER BY RAND()

Though, this appears that it produces the result I need.

I have a new memory.

regarding the TIMESTAMP functions, what is the datatype of your begin and end columns?

because i think the WHERE condition will work fine without the functions

They are timestamps so I changed to:

WHERE NOW() BETWEEN plan.begin AND plan.end

which was successful. Thank-you very much for the tip. Looks like I don’t need to convert to timestamps data that’s already a timestamp.

I love unexpected learning.

Thanks for being so helpful r937.

Instead of using the ORDER BY RAND() , drop that bit and once you’ve got the result set into an array using whatever server-side language you’re using, it’ll be quicker. There is a thread somewhere (either in the PHP or the Database forum), I can’t remember the thread title but the efficiency of different methods for getting a random result set were tested and compared including how well they scaled up (execution time) as the result sets got bigger.