Table 1 uses joining Table2 for a many to many relationship with Table 3.
Table1
ID
Table2
Table1ID
Table3ID
Table3
ID
SomeData
I want a query that will return only 1 instance of a row from Table1 for a search for multiple Table2.ID’s
Table1:
ID
1
2
3
4
Table 2
1 1
1 2
2 2
2 3
Table3
1 “foo”
2 “bar”
3 “whizz”
4 “bang”
Now I can do a join with a subquery
SELECT * FROM Table1
WHERE ID in (
SELECT DISTINCT(T1.ID)
FROM Table1 T1 LEFT OUTER JOIN Table2
ON T1.ID = T2.Table1ID
WHERE T2.Table3ID in (1,2)
)
where 1 and 2 are the values I’m searching for.
Now I want to wrap this up in a view so I can query something like:
Select ID, {other stuff from T1} from vwGetData
where Value in (1,2)
Is this possible? hope I made it clear enough
TIA!
If you need some values from Table1 your query should look like this:
There is no way to create a view of that and allow you to add in the filter on (1, 2) as a where clause to the query of the view.
Your sample query against the view only retrieves the ID column so to get that information you don’t need to involve
Table1in the query.You could create a view of that (without the
distinctpart) but that would not make much sense at all would it?Used like this:
The view does not really add any value.