Connecting Tech Pros Worldwide Help | Site Map

Random records from 2 tables?

Newbie
 
Join Date: Sep 2007
Posts: 27
#1: Sep 18 '07
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
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#2: Sep 18 '07

re: Random records from 2 tables?


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
Newbie
 
Join Date: Sep 2007
Posts: 27
#3: Sep 18 '07

re: Random records from 2 tables?


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
markrawlingson's Avatar
Moderator
 
Join Date: Aug 2007
Location: Bowmanville, Ontario
Posts: 329
#4: Sep 19 '07

re: Random records from 2 tables?


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
Newbie
 
Join Date: Sep 2007
Posts: 27
#5: Sep 19 '07

re: Random records from 2 tables?


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
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#6: Sep 20 '07

re: Random records from 2 tables?


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
markrawlingson's Avatar
Moderator
 
Join Date: Aug 2007
Location: Bowmanville, Ontario
Posts: 329
#7: Sep 20 '07

re: Random records from 2 tables?


If you can't get the NEWID() function to work, I would try doing what Jared suggested, it should work very nicely
Newbie
 
Join Date: Aug 2009
Posts: 4
#8: Aug 19 '09

re: Random records from 2 tables?


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
Reply