multi-table query: returning data when one table has nothing?

  • here is a sample simplified query:

    SELECT A.name, B.address, C.country

    FROM names A, addresses B, countries C

    WHERE A.id=1 AND b.id=1 AND c.id=1

    If there is no row where "b.id=1" then the query returns nothing. But what I would like it to do is return whatever results actually do exist:

    A.name, B.address (as a null), and C.country

    is there an easy way to do this in a single statement? I can copy the items to variables and do 3 queries, but my real world example is huge and not this simplistic.

    thanks for any help.

  • It would help if you would provide the DDL for the tables (CREATE TABLE statements), sample data for each of the tables (a series of INSERT INTO statements), expected results based on the sample data, and the code you have written so far to solve your problem.

    If you provide all this information you will get better answers and tested code in return.

  • Three tables, no joins?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • from your Table Name I think that your Name table is master table and rest of the others are depandant on them. is it the case than

    you can write like this.

    SELECT A.name, B.address, C.country

    FROM names A

    Left join addresses B on A.Id= B.id

    Left join countries C on A.id= C.id

    WHERE A.id=1

  • ignore my last , i didnt see the joins

  • mukti.roy, thank you, perfect

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

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