If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Order By CASE RAND()

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-10-11, 15:02
oldnickj oldnickj is offline
Registered User
 
Join Date: Jan 2009
Posts: 103
Order By CASE RAND()

Realizing that using Rand in order by is a bad habit. Two questions:

1. What is the best way to get random records?

2. In the query below RAND() is not producing random results but sorts the results in some manor.

Here is the URL: https://www.artcardsforcauses.com/ga....php?artists=0

SELECT *, country_art
FROM image_img
LEFT JOIN label ON id_lbl=(theme_img
OR style_img
OR medium_img
OR season_img)
LEFT JOIN artist_art On artist_img=user_art
WHERE approvedIMG=1
AND (private_img <>1
OR `private_img`IS NULL )
AND ((0 =0)
OR (artist_img = 0))
ORDER BY CASE
WHEN artist_img=0 Then RAND()
WHEN artist_img=0 Then cdates_img
END


Thanks
Nick
Reply With Quote
  #2 (permalink)  
Old 03-10-11, 16:22
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Hi Nick,

have you tried something along these lines (it might not be the most efficient way but it will work):

Code:
SELECT *
FROM   (SELECT fields,
               Rand() AS random
        FROM   a1) a
ORDER  BY a.random;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 03-10-11, 20:40
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by oldnickj View Post
What is the best way to get random records?
there is no best way, it depends on your situation

there are many techniques discussed here --
~jk ORDER BY RAND()

by the way, this --
Code:
id_lbl=(theme_img 
OR style_img 
OR medium_img 
OR season_img)
is invalid sql
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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