how to query::join two table, one table

  • How can I join two table (with a query) that, one table has no data and the other has some data!!???

    this is because some times my data base may have a table with or with out data.

    thanks

    I can't use:

    SELECT comp.NAME, car.ID

    FROM comp, car

    it returns an empty table...

  • Does this help you?

    create table dbo.Table1 (

    table1ID int primary key,

    table1Data1 varchar(10),

    table1Data2 varchar(10) null

    )

    create table dbo.Table2 (

    table2ID int primary key,

    table2Data1 varchar(10),

    table2Data2 varchar(10) null

    )

    insert into dbo.Table1

    select 1,'Data1','More Data' union

    select 2,'Data2','Some More'

    select * from dbo.Table1 -- show data

    select * from dbo.Table2 -- show data, none

    select

    t1.table1ID,

    t1.table1Data1,

    t1.table1Data2,

    t2.table2ID,

    t2.table2Data1,

    t2.table2Data2

    from

    dbo.Table1 t1

    inner join dbo.Table2 t2

    on (t1.table1ID = t2.table2ID) -- returns nothing

    select

    t1.table1ID,

    t1.table1Data1,

    t1.table1Data2,

    t2.table2ID,

    t2.table2Data1,

    t2.table2Data2

    from

    dbo.Table1 t1

    left join dbo.Table2 t2

    on (t1.table1ID = t2.table2ID) -- returns all record from table1

    drop table dbo.Table1

    drop table dbo.Table2

    You can learn more about the different joins in Books Online (BOL).

  • IS there a simple way to do that?

  • batista1331 (11/20/2008)


    IS there a simple way to do that?

    :unsure: What could be simplier? If you need to join multiple tables together, you have to use joins of some time and relate the data together some how.

    Using ANSI standard joins is also the best way to do this.

  • use left outer join to get the data in both tables. for more info see Joins SQL BOL.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply