473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Random records from 2 tables?

36
Hi Guys

I think I have an easy one for you.

I am currently using the following script to display records from 2 different tables (SQL server 2007) onto my page.

-------------script--------------
sSQL = "SELECT make, model, orderid, picturelink,regyear, engine, transmission, colour, mileage, price, county, vendor FROM TABLE1"
sSQL = sSQL & " UNION"
sSQL = sSQL & " SELECT makeofcar, requiredmodelofcar, orderid ,orderid, regyear, vehiclecc, transmission, colour, requiredmiles, requiredcarprice, yourcounty, terms FROM TABLE2"
sSQL = sSQL & " WHERE paymentReceived = 'X'"
sSQL = sSQL & " ORDER BY orderid"

--------------end--------------

However, I wish to display random records onto my page from both tables.

My database contains thounsands of records. How is this easily acheived in the most efficient way?

Any help would be fully appreciated

Best regards

Rod from the UK
Sep 18 '07 #1
7 2312
jhardman
3,406 Expert 2GB
Rod,
Expand|Select|Wrap|Line Numbers
  1. ORDER BY NEWID()
(note, this is dependent on the type of db, this is how you would do it in MS SQL Server). Let me know if this works for you.

Jared
Sep 18 '07 #2
rjoseph
36
Hi Jarod

Thank you for your reply.

Unfortunatley, this doesn't work for me.

All I get is the following error:

--------------error-------------
Microsoft OLE DB Provider for SQL Server error '80040e14'

ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator
------------end-------------

My Script now reads:

------------revised script----------------
sSQL = "SELECT make, model, orderid, picturelink,regyear, engine, transmission, colour, mileage, price, county, vendor FROM ClientTable"
sSQL = sSQL & " UNION"
sSQL = sSQL & " SELECT makeofcar, requiredmodelofcar, orderid ,orderid, regyear, vehiclecc, transmission, colour, requiredmiles, requiredcarprice, yourcounty, terms FROM Contact"
sSQL = sSQL & " WHERE paymentReceived = 'X'"
sSQL = sSQL & " ORDER BY NEWID()"
------------revised script end-------------------

If you have any ideas then that would be great.

I look forward to hearing from you

Rod from the UK
Sep 18 '07 #3
markrawlingson
346 Expert 100+
I believe the NEWID() method works with the autonumber / autoincrement field in your table. It doesn't look like you're specifying one in your query(ies). Try putting the autonumber field for each table into both of the select statements.

If that doesn't work then try SELECT * FROM and see if it works when you specify all columns.

I'm 90% sure you'll need an autonumber field for NEWID to work so make sure you have one in there somewhere

Sincerely,
Mark
Sep 19 '07 #4
rjoseph
36
Hi Mark

Thank you for your reply.

I don't think I have an autonumber or autoincrement field in my tables. What do I do now?

Any help would be fully appreciated

best regards

Rod from the UK
Sep 19 '07 #5
jhardman
3,406 Expert 2GB
Rod,

several other DBs have an ORDER BY RAND() (or RANDOM()) option, but I don't think SQL SERVER does. You might have to pull up the records, do a randomize function to get a random number between 1 and the record count, then use the rs.move() method to move to your randomly selected record. Does that suggest a good method?

Jared
Sep 19 '07 #6
markrawlingson
346 Expert 100+
If you can't get the NEWID() function to work, I would try doing what Jared suggested, it should work very nicely
Sep 20 '07 #7
Hi

The reasons and ways to avoid this error have discussed in this

site with good examples. By making small changes in the query

http://sqlerror104.blogspot.com/2009...select_19.html
Aug 19 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Keith Griffiths | last post by:
I'm trying to do a search under a set criteria followed by a selection of random entries meeting this criteria. But I don't seem to be able to achieve this. The idea being to search on say...
3
by: Wim Roffil | last post by:
Hi, When I do a select with a limit I get always the same records. Usually this is the desidered effect. But now I want to do a random select. So every time I do a select I should get a...
2
by: Thomas T. Thai | last post by:
I would like to select a random record from a group of records so I'd end up with one random record per group: CREATE TABLE randtest ( catnum int, title varchar(32) ); INSERT INTO randtest...
3
by: jaYPee | last post by:
anyone know how can i get a 10% random records out of 2000 records? 2000 is only an example and it will vary anytime. i want to create a report out of this 10% thanks in advance
4
by: yf | last post by:
A KB article "http://support.microsoft.com/default.aspx?scid=kb;en-us;209599" tells that the maximum number of records that a table may hold if the PRIMARY key data type is set to AUTONUMBER is...
2
by: IceCube | last post by:
Hello, I would like to select/filter at random 30 records out of an Access-table of 1500 records. I know the option "Top" which gives me the possibility to see the 30 first records of the table...
9
by: Jeremy | last post by:
I have a situation where i am trying to run two query's and retrieve one record from each query, then using Union combine them into one recordset. The First Query is strait forward and should just...
1
by: mykhan | last post by:
Hi I am using access database with asp.net (VB.net for code): I am using the following query to retrieve records from some tables: SELECT...
7
by: FrankEBailey | last post by:
I'm not sure if this is a completely dumb question, but please humor me :) I have a table of records, called Records, each of which has a Category_ID that places it in a specific category; the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.