Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 sql

Author  Topic 

satya068
Posting Yak Master

233 Posts

Posted - 2010-04-12 : 04:33:57
HI

could any one answer this question i got a solution but its not resonable one.


You have 2 tables: tableA and table B. The ID field in tableA maps to the FKtableA
Field in tableB.
Write a SQL Query that would return all the records in tableA and the value the primary key and field1 from tableB where it exists.

tableA definition
ID int primary key,
field1 varchar(10),
field2 varchar(10)

tableB definition
PK int primary key,
field1 varchar(10),
field2 varchar(10),
FKtableA int

thanx



Satya

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 04:46:10
What's the solution you already have, and what's wrong with it?

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

satya068
Posting Yak Master

233 Posts

Posted - 2010-04-12 : 04:48:43
i am using the liftouter join for the tableb could u plz tell ur idea for writing this query plz.

quote:
Originally posted by DBA in the making

What's the solution you already have, and what's wrong with it?

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.



Satya
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 04:54:15
Well, there could be more than one record in Table B for each record in table A, so a LEFT JOIN (which is what I was thinking) would possibly return multiple copies of the records in table A. Is that what you're after?

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

satya068
Posting Yak Master

233 Posts

Posted - 2010-04-12 : 04:58:01
select * from tableA
left outer join tableA.id=tableB.field1

this is the script i gotbut its not working

Satya
Go to Top of Page

satya068
Posting Yak Master

233 Posts

Posted - 2010-04-12 : 05:08:26
could you tell me how to write a script for getting without multiple records form tableB

quote:
Originally posted by DBA in the making

Well, there could be more than one record in Table B for each record in table A, so a LEFT JOIN (which is what I was thinking) would possibly return multiple copies of the records in table A. Is that what you're after?

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.



Satya
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 05:14:40
If each record in table A has multiple records in table B, and you want to see records from table A and columns from table B, but you only want to see each record from table A once, then how do you determine which of the multiple records in table B should be joined to table A?

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

satya068
Posting Yak Master

233 Posts

Posted - 2010-04-12 : 05:19:14
field1 in tableB should join to tableA

could u tell the script by joining this..pls



Satya
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 05:35:02
quote:
Originally posted by satya068

field1 in tableB should join to tableA



Yes, I've got that. The question was "which RECORD" from table B should be joined to those of table A.

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

satya068
Posting Yak Master

233 Posts

Posted - 2010-04-12 : 05:41:09
ALL THE REOCRDS FROM feild1 in tableB

Satya
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-04-12 : 05:47:39


select * from tableA
left outer join tableA.field1=tableB.field1


PBUH
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 05:55:55
quote:
Originally posted by satya068

ALL THE REOCRDS FROM feild1 in tableB

Satya



You're going to have to supply some sample data, and a sample output. What you're asking for doesn't seem to make sense.

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page
   

- Advertisement -