Skip to content
Advertisement

Use empty table in SQL query

I have created a new SQL table of two column [Lot_no, Comment] which is empty.

|Lot_No|comment|

I have another table of two column [Lot_no, Product_Code] which has values.

|Lot_No|Product_Code|
|1     |2304|
|2     |2307|
|3     |2317|
|4     |2308|

Problem:

  1. I want to create a SQL query where I retrieve [Lot_No|Product_Code|Comment] for some [Lot_No] but since no data is available for comment in first table it should return null in comment retrieved column.

  2. Later I want to insert new row of Lot_No and comment in that empty table. How can I do this?

Advertisement

Answer

A simple LEFT JOIN should do the trick. Perhaps something like this:

Select A.Lot_No
      ,A.Product_Code
      ,B.Comment
 From  YourTable A
 Left Join YourCommentTable B
  on  A.Lot_No = B.Lot_No
 Where A.Lot_No = 3 -- for example

For the Insert

Insert Into YourCommentTable (Lot_No,Comment) values (4,'Some new comment')
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement