Solved

select random query in mysql using asp

Posted on 2006-11-20
1
596 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

23 Experts available now in Live!

Get 1:1 Help Now