| Author |
Topic |
|
DestinyJack
Starting Member
22 Posts |
Posted - 2005-08-24 : 05:55:35
|
| I have 2 tables as following:TableA TableB====== ======RecNum RecNumColumn1 Column1Column2 Column2Column3 Column3If I need to select column1, Column2 or Column3 that only exists in either TableA or TableB, how should I write the select statement?Example: if any column in TableA exists only in TableA, show the row.if any column in TableB exists only in TableB, show the row.if any column in exists in both tableA and TableB, do not show the row. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-24 : 06:06:08
|
| Do you want to show the records that dont exists in both the tables?Can you give us some sample data and the result you want?MadhivananFailing to plan is Planning to fail |
 |
|
|
DestinyJack
Starting Member
22 Posts |
Posted - 2005-08-24 : 06:09:49
|
| Yes, I want to show the records that don't exists in both the tables.TableA======Column1 Column2 Column3Apple Orange FruitMeat Fish SeafoodTableB======Column1 Column2 Column3Car Ship TransportMeat Fish SeafoodSo the result will show:Car Ship TransportApple Orange Fruit |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-08-24 : 06:11:59
|
[code]select t1.*, t2.*from tableA t1 left join TableB t2 on t1.Column1 = t2.Column1 and t1.Column2 = t2.Column2 and t1.Column3 = t2.Column3where t2.Column1 is null and t2.Column2 is null and t2.Column3 is null[/code]Go with the flow & have fun! Else fight the flow |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-24 : 06:20:53
|
| Try thisSelect * from(Select t1.*,t2.column1 as col1, t2.column2 as col2, t2.column3 as col3 from table1 T1 left join table2 T2 on t1.column1=t2.column1 and t1.column2=t2.column2 and t1.column3=t2.column3) Twhere col1 is nullMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-24 : 06:22:31
|
Mladen, When I posted I didnot see your replyYours is more clearer than mine MadhivananFailing to plan is Planning to fail |
 |
|
|
DestinyJack
Starting Member
22 Posts |
Posted - 2005-08-24 : 06:42:00
|
| The result I need will contains rows that doesn't exists in both table, meaning if it exists in TableA and not TableA, or if it exists in TableB but not in TableA.select t1.*, t2.*from tableA t1 left join TableB t2 on t1.Column1 = t2.Column1 and t1.Column2 = t2.Column2 and t1.Column3 = t2.Column3where t2.Column1 is null and t2.Column2 is null and t2.Column3 is nullThe statement above will only show records that exists in TableA but not in TableB. It didn't show records that exists in TableB but not in tableA. But it give me some clues for the solution:select t1.*, t2.*from tableA t1 full outer join TableB t2 on t1.Column1 = t2.Column1 and t1.Column2 = t2.Column2 and t1.Column3 = t2.Column3where (t1.Column1 is null and t1.Column2 is null and t1.Column3 is null)or(t2.Column1 is null and t2.Column2 is null and t2.Column3 is null)This is what I modified and it give me the result I want, but in the following format:Column1 Column2 Column3 Column1 Column2 Column3 NULL NULL NULL Car Plane TransportApple Orange Fruit NULL NULL NULLIs there anyway to remove all the column with "NULL"?Thanks a lot for the helps. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-24 : 06:55:26
|
| If you want to remove Null and show empty space then specify the columns in Select query as isnull(column,'')MadhivananFailing to plan is Planning to fail |
 |
|
|
|