Buy Sell Discuss UK Domain Names at AcornDomains.co.uk

Today's Drop Dates are: 19-02-2012 or 26-02-2012   All times are GMT. The time now is 12:45:27 PM.
Domain Name Sales Domain Software Calculate UK Domain Drop Dates Domain Registration NameDrive Domain Parking Subscribe to our Domains For Sale newsletter
Go Back   Domain Forum Acorn Domains Buy Sell Auction UK Domains > Website Design and Promotion > Website Design > Scripts and Coding
Connect with Facebook

Scripts and Coding PHP, MySQL, scripts

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 08-07-2011, 02:07:59 PM     #1 (permalink)

 
Join Date: Jul 2008
Posts: 454
GeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond repute

Transact SQL Help

I am actually using SQL Server 2005, but I am sure this is such a fundamental simple problem that any Transact SQL would do. If anyone knows the answer I'd really appreciate the help - I am missing something very simple here!

I have 3 or more tables with various columns, but all having 1 column in common. I want to perform a query that picks the top x records, or from x+1 to y, from table 1 when ordered by the common column, then data from table 2 and table 3 using the same column. I can do it with 2 tables using Top and a Join, but struggle extending it beyond 2 tables. So I currently have:


SELECT TOP 6 table1.CommonColumn, table1.AnotherColumn, table2.DifferentColumn1, table2.DifferentColumn2

FROM table1

Join table2

On table1.CommonColumn = table2.CommonColumn

WHERE table1.SomeColumn = Value

AND table1.CommonColumn NOT IN (SELECT TOP 12 table1.CommonColumn

FROM table1

WHERE table1.SomeColumn = Value)

order by table1.CommonColumn



I need to extend it to return table3.DifferentColumn3, table3.DifferentColumn4 with table1.CommonColumn = table3.CommonColumn. It's POETS day and I would rather be sloping off to the pub for a sneaky pint. Any help very gratefully received....
GeoMal is offline  
Old 08-07-2011, 05:47:42 PM     #2 (permalink)

 
Join Date: Dec 2007
Posts: 123
GlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant future

Hi, how about doing this instead of joining the tables?

1 create a temp table
2 insert x rows from table 1
3 insert x rows from table 2
4 insert x rows from table 3
5 select * from temp table
GlasgowBob is offline  
Old 08-07-2011, 06:02:24 PM     #3 (permalink)

 
Join Date: Jul 2008
Posts: 454
GeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond repute

Quote:
Originally Posted by GlasgowBob View Post
Hi, how about doing this instead of joining the tables?

1 create a temp table
2 insert x rows from table 1
3 insert x rows from table 2
4 insert x rows from table 3
5 select * from temp table
Hmm, ok. The first query is easy, it's a subset of my query above. I would then need to query table2 based upon the CommonColumn in the temporary table, and not insert extra rows but update the existing rows in the temp table inserted from table1 with columns from table2, and then repeat. Is this just a query on table2 with a subquery using CommonColumn in the temp table? My transact sql is crap.

Thanks very much for the reply, I'll give it a go! Cheers Bob.
GeoMal is offline  
Old 08-07-2011, 06:25:29 PM     #4 (permalink)

 
Join Date: May 2007
Posts: 853
stevebrowne has a reputation beyond reputestevebrowne has a reputation beyond reputestevebrowne has a reputation beyond reputestevebrowne has a reputation beyond reputestevebrowne has a reputation beyond reputestevebrowne has a reputation beyond reputestevebrowne has a reputation beyond reputestevebrowne has a reputation beyond reputestevebrowne has a reputation beyond reputestevebrowne has a reputation beyond reputestevebrowne has a reputation beyond repute

SELECT TOP 6 table1.*, table2.*, table3.*

FROM table1

Join table2 On table1.CommonColumn = table2.CommonColumn
Join table3 On table1.CommonColumn = table3.CommonColumn

WHERE table1.SomeColumn = Value

etc..


you might need FULL OUTER JOIN instead of just join if you don't have the same commoncolumn in all 3 tables but still want a reecord returned.
stevebrowne is offline  
Old 08-07-2011, 06:28:51 PM     #5 (permalink)

 
Join Date: Dec 2007
Posts: 123
GlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant futureGlasgowBob has a brilliant future

Sorry sounds like your trying to do something more complicated than I thought. I'm not that great with SQL either so hope you get the answer
GlasgowBob is offline  
Old 09-07-2011, 07:36:23 AM     #6 (permalink)

 
Join Date: Jul 2008
Posts: 454
GeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond reputeGeoMal has a reputation beyond repute

Quote:
Originally Posted by stevebrowne View Post
SELECT TOP 6 table1.*, table2.*, table3.*

FROM table1

Join table2 On table1.CommonColumn = table2.CommonColumn
Join table3 On table1.CommonColumn = table3.CommonColumn

WHERE table1.SomeColumn = Value

etc..

you might need FULL OUTER JOIN instead of just join if you don't have the same commoncolumn in all 3 tables but still want a reecord returned.
Steve, many thanks you are a whiz. I had previously tried what you stated but it was only returning a single record. I now realise it was only returning records that existed in table3, whereas I wanted a return of table1 and table2 even if there was no match in 3 - your FULL OUTER JOIN did just this. Result.

Thanks again.
GeoMal is offline  
Closed Thread



Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Domain Name Community Replies Last Post
Experienced .Net / SQL Developer cm1975 Services Wanted 0 15-09-2009 09:58:11 AM
SQL Injection Problem stevebrowne General Board 5 17-06-2008 07:47:43 PM
Blocking Web Sites in ISA Server - SQL Server Magazine (subscription) RSS Domain Name News 0 26-12-2006 03:59:06 PM
sql injection, watch this ONExFOUR General Board 10 30-10-2006 01:26:33 AM

Web Hosting from UK2 from just 99p


All times are GMT. The time now is 12:45:27 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.6.0 RC 2
All content on Acorn Domains is member generated and is not moderated before posting. All content is viewed and used by you at your own risk and AD does not warrant the accuracy or reliability of any of the information. The views expressed are those of the individual contributors and not necessarily those of AD. Please contact us to report any issues or send a PM to "Admin".