Not sure if the question is phrased correctly but here's an example of the schema:
CREATE TABLE TabA (Id INT NOT NULL, PRIMARY KEY (Id));
CREATE TABLE TabB (
Id INT NOT NULL,
TabAId INT,
PRIMARY KEY (Id),
FOREIGN KEY (TabAId) REFERENCES TabA(Id)
)
CREATE TABLE TabC (
Id INT NOT NULL,
TabAId INT,
PRIMARY KEY (Id),
FOREIGN KEY (TabAId) REFERENCES TabA(Id)
)
What I need to do is to select TabA.Id
where TabA.Id
is either referenced by records in TabB
or TabC
(or both). If I were to translate this programmatically:
type B = { Id : int }
type C = { Id : int }
type A = { ItemsB : B list ; ItemsC : C list }
let itemsA = [ ... ] // a list of items A
let filtered = itemsA |> List.filter (fun a -> a.ItemsB.Length > 0 || a.ItemsC.Length > 0)
So far I tried this query, but I'm not sure if it's good:
SELECT a.Id
FROM TabA a
INNER JOIN
(
SELECT b.TabAId AS tabAId
FROM TabB b
FULL OUTER JOIN TabC c ON b.TabAId = c.TabAId
) sub
ON a.Id = sub.tabAId
Any thoughts? Thank you.
SELECT a.Id
instead of all the columns.