|
-
Jul 18th, 2005, 07:28 AM
#1
Thread Starter
Hyperactive Member
'OR' between tables
I have 2 tables say tableA and tableB
tableA has columns - A,B,C1
tableb has columns - A,B,C2
I need rows from tableA and tableB - A,B,C1,C2
when data is there is either in C1 or C2 or both.
Please suggest how do I write such query.
thanks
-
Jul 18th, 2005, 07:42 AM
#2
Addicted Member
Re: 'OR' between tables
hi
check whether this will work.,
select TA.A, TA.B, TA.C1, TB.C2 from (select A, B, C1 from tableA where C1 is not null) as TA, (select A, B, C2 from tableB where C2 is not null) as TB
You can do it using joins also.
Last edited by kishore.kr; Jul 18th, 2005 at 07:54 AM.
--Kishore...
-
Jul 18th, 2005, 07:50 AM
#3
Thread Starter
Hyperactive Member
Re: 'OR' between tables
can't do TA.A = TB.A and TA.B = TB.B
since data may or may not be same, column name A, B are same C1 and C2 are diff. Primary key data may/may not be same.
-
Jul 18th, 2005, 08:01 AM
#4
Addicted Member
Re: 'OR' between tables
hi
try union,
(select * from tableA where C1 is not null)
union all
(select * from tableB where C2 is not null)
--Kishore...
-
Jul 18th, 2005, 08:03 AM
#5
Re: 'OR' between tables
Code:
Select aryall.a, qryall.b,table1.c as c1, table2.c as c2
From
((
select table1.a, table1.b from table1
union
select table2.a, table2.b from table2
) as qryAll Choices
left join table1 on qryAll.a=table1.a and qryall.b=table1.b)
left join table2 on qryAll.a=table2.a and qryall.b=table2.b
Something like that perhaps?
The union provides a full list (I think) of non duplicated a's and b's
The two joins pull in the columns c.
Another alternative is to use a union to pull all those from table 1 with something in C and union it to all those from table 2 with something in C:
Code:
Select table1.a, table1.b, table1.c from table1 where not table1.c is null
union
Select table2.a, table2.b, table2.c from table2 where not table2.c is null
Edit:
Kishore beat me there! lol
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Jul 18th, 2005, 08:09 AM
#6
Addicted Member
Re: 'OR' between tables
hi ,
i think we have to use union all instead of union ( if we are going to second option..). the first qry is little bit complicated for me to understand but its good to learn from queries like these..
--Kishore...
-
Jul 18th, 2005, 09:02 AM
#7
Thread Starter
Hyperactive Member
Re: 'OR' between tables
The first query works great, although I didn't get it fully - pretty complex for me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|