Home > Software engineering >  How do I get data from another column when I've already used JOIN
How do I get data from another column when I've already used JOIN

Time:01-06

Let's say I have two tables:

Table 1 has columns: ID, Product, Name

Table 2 has columns: ProductID, Start Date, End Date

ID and Product ID are the same, how do I get Start Date and End Date in the first table?

This is the code I've used:

SELECT 
        table1.Name, 
        table1.ID, 
        table1.Product 
  FROM table1
  JOIN table 2
  ON table1.ID=table2.ProductID

How can I also get the Start Date and End date from table2 in this query?

CodePudding user response:

You can just add the table names to the list in the SELECT:

SELECT 
      table1.Name, 
      table1.ID, 
      table1.Product,
      table2.StartDate,
      table2.EndDate
FROM 
  table1
  JOIN table2
    ON table1.ID = table2.ProductID
  •  Tags:  
  • Related