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 > random?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-29-04, 09:23
glyndower glyndower is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
random?

////////$sql = "SELECT * FROM agentdb WHERE ((OfficeID <> 214) and (agentdb.agent_id<> 1115421) and (agentdb.agent_id > 100) and agentdb.agent_id <> 333)) ORDER BY RAND() LIMIT 1";

The above code should generate a random agent upon refresh.

It does however seem to favor one agent more than the others. The agent id

it used to favor was "333" i added the "(agentdb.agent_id <> 333)" to

disallow that agent from being displayed. But now it wants to favor an agent

whose id number is "1167553"

The function can be seen here: http://c21alliancerealty.com/area.php it is

the random "Featured Agent" on the left hand side of the page.

Does anyone have any suggestions as to why this is occuring, please?
Reply With Quote
  #2 (permalink)  
Old 01-29-04, 13:15
cardillac cardillac is offline
Registered User
 
Join Date: Jan 2004
Posts: 2
Re: random?

I can't tell why your query behaves like that, but in a situation similar to yours I tried the one below and it worked fine for me:

SELECT agentdb.agent_id,..., MD5(RAND()*NOW()) AS myrand
FROM agentdb
WHERE .... ORDER BY myrand LIMIT 1
Reply With Quote
  #3 (permalink)  
Old 01-29-04, 16:11
glyndower glyndower is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
expand, please?

Hi cardillac .

Thanks for your response. Could you expand on your code snippet a bit please? I must be missing something when I fill in the blanks.
Reply With Quote
  #4 (permalink)  
Old 02-27-04, 08:11
cardillac cardillac is offline
Registered User
 
Join Date: Jan 2004
Posts: 2
Re: expand, please?

hi glyndower,

sorry that I did not reply to your question a month ago. in the meantime you surely found out what I meant. I post it anyway for others, who would like to know:

SELECT * does not work if you need one userdefined column like 'MD5(RAND()*NOW()) AS myrand', so you have to specify all the needed fields. 'AS myrand' creates an Alias for that column to be able to address it in the ORDER BY clause. the function rand() generates a number, multiplied by the timestamp value of the actual time. MD5 generates an 16bit-integer of this product. The resulting integer will be unique, so you can sort the result by 'myrand' and hopefully always get a different record as first and only record.

the ... were meant as the desired field names / conditions.
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