Results 1 to 7 of 7

Thread: 'OR' between tables

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    320

    '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

  2. #2
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    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...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    320

    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.

  4. #4
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    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...

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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

    BOFH Now, BOFH Past, Information on duplicates

    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...

  6. #6
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    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...

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    320

    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
  •  



Click Here to Expand Forum to Full Width