Solved

select random query in mysql using asp

Posted on 2006-11-20
1
595 Views
Last Modified: 2009-10-27
I want to select random records from mysql database returning a set amount of results, I have got it working but it also generates no fixed amount of records returned. Can anyone suggest what I have done wrong.

Here is my code

<%
                do

                    randomize timer()
                    dim start_marker
                    start_marker = rnd() * 1000000

                    strSQL = "select offers.id, manufacturers.manufacturers,  models.model, contract_name, "
                    strSQL = strSQL & "offer_ppm as price "
                    strSQL = strSQL & "from offers "
                    strSQL = strSQL & "join manufacturers on offers.offer_manufacturer = manufacturers.id "
                    strSQL = strSQL & "join models on offers.offers_model = models.id "
                    strSQL = strSQL & "join finance_type on offers.offer_finance = finance_type.id "
                    strSQL = strSQL & "where random_number > '" & start_marker & "' and offer_finance <> '1' "
                    strSQL = strSQL & "order by random_number"

                    RecordSet.open strSQL, dbConn

                    if not(RecordSet.EOF and RecordSet.EOF) then
                        exit do
                    else
                        recordset.close
                    end if
                Loop

                %>

Thanks
0
Comment
Question by:marcusah
1 Comment
 
LVL 12

Accepted Solution

by:
AdrianSRU earned 250 total points
ID: 17981495
Try this:

strSQL = "select offers.id, manufacturers.manufacturers,  models.model, contract_name, "
    strSQL = strSQL & "offer_ppm as price "
    strSQL = strSQL & "from offers "
    strSQL = strSQL & "join manufacturers on offers.offer_manufacturer = manufacturers.id "
    strSQL = strSQL & "join models on offers.offers_model = models.id "
    strSQL = strSQL & "join finance_type on offers.offer_finance = finance_type.id "
    strSQL = strSQL & "where offer_finance <> '1' "
    strSQL = strSQL & "ORDER BY RAND() LIMIT 10"

"ORDER BY RAND()" will randomize the order of the results.
"LIMIT 10" will return the first 10 records of the result set.  Since the records are randomized you will get 10 random records.  All you have to do is change 10 to the number of records that you want.


--Adrian
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now