| Author |
Topic |
|
y0zh
Yak Posting Veteran
60 Posts |
Posted - 2010-04-23 : 09:54:22
|
| There are 2 tablestab1IDname Name1 Ivanov2 Petrov3 Sidorov4 Mihalych5 Alekseevtab2IDname Name1 Ivanov2 Petrov3 Sidorov5 Alekseev6 NikiforovCould you provide me a query that finds records that exist in tab1 and does not exist in tab2 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-23 : 09:58:38
|
| SELECT * FROM tab1EXCEPTSELECT *FROM tab2 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-23 : 10:01:34
|
| [code]SELECT * FROM tab1 t1WHERE NOT EXISTS(SELECT 1 FROM Tab2 WHERE Name = t1.Name)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-23 : 10:03:17
|
alsoSELECT t1.*FROM Tabl1 t1LEFT JOIN Tab2 t2ON t1.Name = t2.NameWHERE t2.Name IS NULL ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
y0zh
Yak Posting Veteran
60 Posts |
Posted - 2010-04-23 : 10:38:37
|
| Tank you guys. You really helped me.BTW This one doesn't works:SELECT * FROM tab1EXCEPTSELECT *FROM tab2Server: Msg 156, Level 15, State 1, Line 3Incorrect syntax near the keyword 'EXCEPT'. |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-23 : 10:42:53
|
| It is because you use SQL Server 2000.Try this one:SELECT IDname, NameFROM (SELECT *, k=1 FROM tbl1 UNION ALL SELECT *, k=2 FROM tbl2) DGROUP BY IDname, NameHAVING MAX(k) = 1; |
 |
|
|
y0zh
Yak Posting Veteran
60 Posts |
Posted - 2010-04-23 : 10:46:14
|
| This query runs on what version of Server?SELECT * FROM tab1EXCEPTSELECT *FROM tab2Thanks, last quesry helped me too |
 |
|
|
y0zh
Yak Posting Veteran
60 Posts |
Posted - 2010-04-23 : 10:47:39
|
| Could we receive the result with other types of?joinMaybe right,outer,etc? |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-23 : 10:47:57
|
quote: Originally posted by y0zh This query runs on what version of Server?SELECT * FROM tab1EXCEPTSELECT *FROM tab2Thanks, last quesry helped me too
SQL Server 2005 introduced this set operator. |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-23 : 10:50:01
|
quote: Originally posted by y0zh Could we receive the result with other types of?joinMaybe right,outer,etc?
Visakh posted solution using LEFT OUTER JOIN in third reply! |
 |
|
|
y0zh
Yak Posting Veteran
60 Posts |
Posted - 2010-04-23 : 10:50:25
|
| ANy other result with join?Maybe together with sub-query? |
 |
|
|
y0zh
Yak Posting Veteran
60 Posts |
Posted - 2010-04-23 : 10:53:36
|
| I mean right join. Is it possible? |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-23 : 11:07:08
|
quote: Originally posted by y0zh I mean right join. Is it possible?
Yes. You should replace the tables name like this:SELECT t1.*FROM Tab2 t2RIGHT OUTER JOIN Tab1 t1ON t1.Name = t2.NameAND t1.IDname = t2.IDnameWHERE t2.Name IS NULL |
 |
|
|
y0zh
Yak Posting Veteran
60 Posts |
Posted - 2010-04-23 : 11:56:27
|
| Thank you. Now I clearly understand how join works. |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-23 : 13:50:04
|
quote: Originally posted by y0zh Thank you. Now I clearly understand how join works.
That is cool |
 |
|
|
|