Solved

select random query in mysql using asp

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Creating and Managing Databases with phpMyAdmin in cPanel.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 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