I have a three-level SQL table tree; parent, child, orphan child.
I wish to select one row from parent i.e. parent key "ID"=3
Then select all rows in table1 child where child1 key "ID1"=3 IF....
Orphan child table2 is a list of "ID2" which act as a filter for "ID1"
I have attempted a left join:
SELECT [itemName]
FROM [dbo].[table]
WHERE [ID] = 3
LEFT OUTER JOIN [dbo].[table1]
ON [dbo].[table].[ID] = [dbo].[table1].[ID1]
The where is breaking it and I'm not even close to figuring out the 3rd level filtering of table1.
These are my table definitions.
CREATE TABLE [dbo].[table] (
[ID] INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
[Company] varchar(250)
);
CREATE TABLE [dbo].[table1] (
[t1_fid] INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
[Product] varchar(250),
[Description] varchar(250),
[ID1] INT FOREIGN KEY REFERENCES [dbo].[table]([ID]) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE [dbo].[table2] (
[t2_fid] INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
[someValue] INT,
[ID2] INT FOREIGN KEY REFERENCES [dbo].[table]([t1_fid]) ON UPDATE CASCADE ON DELETE CASCADE
);
My tables are as such.
table:
[ID] [Company]
1 companyA
2 companyB
3 companyC
table1:
[t1_fid] [Product] [Description] [ID1]
1 Tree Green 3
2 House Built 1
3 Tree Dead 3
4 Car White 2
5 House Sold 3
6 Car Crashed 3
7 Car Sold 3
table2:
[t2_fid] [someValue] [ID2]
1 60 1
2 2 2
3 15 5
4 0 6
So here is the result table I would like. It is a table of all companyC Products which also exist in table2.
[Company] [Product] [Description] [someValue]
companyC Tree Green 60
companyC House Sold 15
companyC Car Crashed 0
I hope this enough information!