Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Random Pulls

    Good day. I am using an MS Access database.

    In one table I will have an unknown number of entries. What I would like to
    do is pull five unique, random entries and display them. The idea is to pull
    five random links from a table of URLs and display them (again, ensuring no two
    displayed links are alike).

    Any suggestions?

    Thank you for your time!

    SMRieth Guest

  2. Similar Questions and Discussions

    1. Creating a page that pulls from a table
      I want to create a page that will dynamically pull information from a single table when querried by a single record. I don't want it to display any...
    2. Server pulls pages from wrong directory
      Hi, We're having a very strange issue. We just upgrade to Coldfusion 6.1, and now it is at random displaying the wrong pages. Particularly, we...
    3. need ftp-mirroring software, that pushes, rather than pulls
      Hello! I need to push a sizable subtree over to another server periodically. The remote, however, only allows ftp... All of the...
    4. Using Math.random to go to random frames
      Hello everyone. My cerebral density is preventing me from seeing the solution to this problem. I have the following code which causes the user to...
    5. Random image in a random place.
      Anyone know javascript? I have a grid(4 x 4) of 16 spacer images and a few text links on the side. Each text link represents a different folder of...
  3. #2

    Default Re: Random Pulls

    SELECT yourfields
    FROM yourtable
    WHERE yoursearchcriteria
    ORDER BY RND(youruniqueidfield)

    HTH

    --
    Tim Carley
    [url]www.recfusion.com[/url]
    [email]info@NOSPAMINGrecfusion.com[/email]
    Mountain Lover Guest

  4. #3

    Default Re: Random Pulls

    Originally posted by: Newsgroup User
    SELECT yourfields
    FROM yourtable
    WHERE yoursearchcriteria
    ORDER BY RND(youruniqueidfield)


    I would also make that .... SELECT TOP 5 yourFields .... to limit the
    returned results to 5 records.


    mxstu Guest

  5. #4

    Default Re: Random Pulls

    Mountain Lover,

    What version of Access/MX are you using? I'm not really that up on Access,
    but I know this technique worked in previous CF versions. Yet it doesn't seem
    to randomize the records when used with CFQUERY, Access 2000 and MX 6.1.



    mxstu Guest

  6. #5

    Default Re: Random Pulls

    I have tried the usual SQL pulls that have been suggested, but they do not
    provide the random pull that I am looking for; they only pull the top 5
    entries. I can pull five random entries between CFML and SQL, but I can't
    figure out how to make them unique without what is probably excessive code (a
    bunch of IF THEN or CASE statements).

    I use MS Access 2003.

    SMRieth Guest

  7. #6

    Default Re: Random Pulls

    >
    > What version of Access/MX are you using?
    CF5, CF6.1 & CF 7 & Access 2000. Yeah you are right, it doesn't
    randomize anymore in CFMX (6&7), CF 5 works perfectly. Gotta' love what
    those JAVA drivers did for the M$ Access connection! :(

    So something like this will get one random value... should work in CFMX
    (untested)
    random_element = RandRange(1, queryname.RecordCount);
    random_value = queryname.columnname[random_element] ;
    you'll need to loop through the query to make a list of 5

    Or maybe this:
    [url]http://www.cflib.org/udf.cfm?id=524&enable=1[/url]

    HTH

    --
    Tim Carley
    [url]www.recfusion.com[/url]
    [email]info@NOSPAMINGrecfusion.com[/email]
    Mountain Lover Guest

  8. #7

    Default Re: Random Pulls

    >... What I would like to do is pull five unique, random entries
    In spite of the simplicity of this statement, I do believe that it is an exceedingly
    difficult problem to solve.
    BKBK Guest

  9. #8

    Default Re: Random Pulls

    Originally posted by: Newsgroup User
    Or maybe this:
    [url]http://www.cflib.org/udf.cfm?id=524&enable=1[/url]


    Mountain Lover,

    I think the UDF would probably work here, if the OP just needs to display 5
    random records each time a page is loaded. Of course your original query idea
    was better ;-)





    <---- NEED TO DOWNLOAD QueryRandomRows() UDF from cflib.org ---->
    <!--- get distinct "links" --->
    <cfquery name="getRecords" datasource="yourAccessDSN">
    SELECT DISTINCT theLink
    FROM yourTable
    </cfquery>

    <!--- get 5 random records --->
    <cfset randRecords = QueryRandomRows(getRecords, 5)>
    <cfdump var="#randRecords#">

    mxstu Guest

  10. #9

    Default Re: Random Pulls

    >... I think the UDF would probably work here
    Anyone have any ideas how the problem can be solved using SQL alone?
    That would be neat.
    BKBK Guest

  11. #10

    Default Re: Random Pulls

    Thanks to everyone for their help.

    I used the longer code I was thinking of before as it seems there is no easier
    way to accomplish this.

    Basically, I created a second table just to hold the random links. I
    generated a random number from 1 up to the total number of links available in
    the original table and queried for the link that had an ID that matched the
    random number. Then, I ran a query to ensure that ID did not exist in the
    second table. If the link did not exist, then it would be entered; if it did
    exist, the system will loop until it finds one that doesn't until there are 5
    records in the second table.

    Oh well, it works. Thanks!

    SMRieth Guest

  12. #11

    Default Re: Random Pulls

    >... there is no easier way to accomplish this.
    This is easy enough, hoping it works. I followed the logic in

    [url]http://techrepublic.com.com/5100-9592_11-5796605.html?tag=sc[/url]





    /*Assuming your table has column called 'url', of type varchar and non-null*/
    CREATE TABLE MyNewTable(PK uniqueidentifier NOT NULL DEFAULT NewID(), url
    VARCHAR NOT NULL);
    SELECT DISTINCT TOP 5 url, NewID() AS Random
    FROM urlTable
    ORDER BY Random; DROP TABLE MyNewTable

    BKBK Guest

  13. #12

    Default Re: Random Pulls

    Unfortunately Access doesn't support NewID(), SQL Server only. And it
    works very well in SQL Server.


    --
    Tim Carley
    [url]www.recfusion.com[/url]
    [email]info@NOSPAMINGrecfusion.com[/email]
    Mountain Lover Guest

  14. #13

    Default Re: Random Pulls

    >... Unfortunately Access doesn't support NewID(), SQL Server only.
    Oops!
    BKBK Guest

  15. #14

    Default Re: Random Pulls

    >>This then makes Rnd() a good replacement for MS SQL's NewID().

    No. Using Rnd(IDColumn) was the original suggestion by Mountain Lover, but if
    you read the thread, you will see that it does not return the anticipated
    results in MX 6.1/7, which is what brought on the search for alternative
    methods.
    >>CREATE TABLE MyNewTable(Random FLOAT NOT NULL, url VARCHAR NOT NULL);
    >>SELECT DISTINCT TOP 5 url, Rnd(rowNr) AS Random
    >>FROM urlTable
    >>ORDER BY Random; DROP TABLE MyNewTable
    If the code is just selecting from "urlTable", what is the purpose of the
    CREATE TABLE statement here? Also, you cannot ORDER BY an alias in Access.

    mxstu Guest

Posting Permissions

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

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