Solved

select random query in mysql using asp

Posted on 2006-11-20
1
599 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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