Search code examples
sqldatatablesinner-join

SQL Statement to Return Results from Two Tables


I tried searching for this question but I don't think that I am wording it properly.

I have two tables, one for cases and one for hardware ordered, the hardware ordered table stores who placed the order, the date ordered, etc but I only want one column from this table which is Hardware Ordered, Yes or No.

The way my mind is thinking right now is that I will query the cases table for all the fields I want and then I want to add the one column from the hardware ordered table.

I tried an inner join but it is not returning anything.

SELECT Cases.CreatedBy
       , Cases.CreatedByEmail
       , Cases.CreatedByID
       , Cases.CaseID
       , HardwarePart.Ordered
FROM Cases 
INNER JOIN HardwarePart 
   ON Cases.RecID = HardwarePart.RecID
WHERE 
   Cases.CreatedDateTime BETWEEN '2019-01-01 00:00:00.000' AND '2019-03-13 23:59:59.999'

Solution

  • I think your problem is on the relation between the two tables INNER JOIN HardwarePart ON Cases.RecID = HardwarePart.RecID

    is Cases.RecID a primary key in the table Cases and HardwarePart.RecID is the foreign key in HardwarePart which connects with Cases?