Results 1 to 4 of 4

Thread: random?

  1. #1
    Join Date
    Jan 2004
    Posts
    4

    Unanswered: 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?

  2. #2
    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

  3. #3
    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.

  4. #4
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •