Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

select random query in mysql using asp

Posted on 2006-11-20
1
Medium Priority
?
601 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 750 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 your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

670 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