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.
| Author |
Topic |
|
aditya2010
Starting Member
10 Posts |
Posted - 2010-03-29 : 01:45:24
|
| Hi,i have 3 tables.I want to retrive data from one table say 'A'.While retriving that data from 'A',i had to write a condition to check if a column say'Col1' from 'A' exists either in other 2 tables say 'B' and 'C'.If exists retrive 'Col1' else not.Please help me in witing this query. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-29 : 02:04:36
|
| [code]SELECT *FROM A aWHERE EXISTS (SELECT 1 FROM (SELECT Col1 FROM B UNION SELECT Col1 FROM C )t WHERE Col1 = a.Col1)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
aditya2010
Starting Member
10 Posts |
Posted - 2010-03-29 : 18:05:56
|
| Thanks Vishak16.That worked.And now if i wanted to add one more condition to the above query u helped me.Say Col2 belongs to 'A' and col2 should not be in table 'B' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-30 : 13:18:21
|
do you mean this?SELECT *FROM A aWHERE EXISTS (SELECT 1 FROM (SELECT Col1 FROM B UNION SELECT Col1 FROM C )t WHERE Col1 = a.Col1)AND NOT EXISTS(SELECT 1 FROM B WHERE Col2=a.Col2) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|