Search code examples
sqloraclecrystal-reportsoracle10g

How to join table in sql without removing data from table1


For every person in column D with number in it.

I need to find person's last name from 2nd table.

select
    a.TIME_STAMP,
    a.RDATE,
    a.TYPE,
    a.PERSON
from table1 a

a pic table2 has person's lastname and field is called last

I tried

select
    a.TIME_STAMP,
    a.RDATE,
    a.TYPE,
    a.PERSON,
    b.last,
from table1 a, table b 
where a.PERSON = b.PERSON 

and it outputs following by removing ABC_CHANGE from the result

b pic

How can I fix query so it won't remove ABC_CHANGE.

I am doing this in crystal report just for extra information but it does same in SQLPlus.


Solution

  • If you want all rows in table A, even if there isn't a match in table B, you need to use a LEFT OUTER JOIN:

    select
        a.TIME_STAMP,
        a.RDATE,
        a.TYPE,
        a.PERSON,
        b.last,
    from table1 a
    LEFT OUTER JOIN table b ON a.PERSON = b.PERSON