I need to create dynamic INNER JOIN (I believe I need to anyway). I have 4 tables pears, apples, oranges, bananas. I used all the same column names in those 4 tables.
Another 2 tables 'fruit' and 'fruitcomments'
pears apples oranges bananas
-------------------------------------
id
user
photo
comment
date
For the Fruits table typeid relates to other tables 1=pear, 2=apples, 3=oranges, 4=banana. Itemid correlates to the id of either pears, apples, oranges or bananas.
Fruit
-----------------------------------------------------------------------------
fruitsid
typeid
itemid
How would I go about selecting the top 10 from the fruit table and dynamically get the matching row from the corresponding table
Working Code
SELECT TOP (10) fruitId, id, user, photo, comment FROM
(
SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment
FROM FRUITS T1 LEFT JOIN PEARS T2 ON (T1.itemId=T2.id)
WHERE T1.typeId=1
UNION ALL
SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment
FROM FRUITS T1 LEFT JOIN APPLES T2 ON (T1.itemId=T2.id)
WHERE T1.typeId=2
UNION ALL
SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment
FROM FRUITS T1 LEFT JOIN ORANGES T2 ON (T1.itemId=T2.id)
WHERE T1.typeId=3
UNION ALL
SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment
FROM FRUITS T1 LEFT JOIN BANANAS T2 ON (T1.itemId=T2.id)
WHERE T1.typeId=4
)a
ORDER BY fruitId DESC
OLD NON-Working Code:
SELECT TOP (10) id, user, photo, comment FROM
(
SELECT f.typeid, f.itemid, a.id, a.user, a.photo, a.comment FROM Fruit f INNER JOIN a on f.item=a.id
--excuse the gibberish. I never used CASE before
case when f.typeid=1 then
(SELECT id, user, photo, comment FROM Pears WHERE id=f.itemid)a
case when f.typeid=2 then
(SELECT id, user, photo, comment FROM Apples WHERE id=f.itemid)a
case when f.typeid=3 then
(SELECT id, user, photo, comment FROM Oranges WHERE id=f.itemid)a
case when f.typeid=4 then
(SELECT id, user, photo, comment FROM Bananas WHERE id=f.itemid)a
)
Something like that, but I dont know how to express it correctly. Expected results below by selecting top (3) from following 'fruit' table order by fruitsid DESC.
fruits
-----------
fruitsid typeid itemid
22 1 19
23 3 73
24 2 46
pears
--------------
id user photo comment date
19 tom 1.jpg hi 2012-06-01 12:00:00.000
22 bill 5.jpg hello 2012-06-01 13:00:00.000
apples
--------------
id user photo comment
46 sam 78.jpg howdy
22 bill 5.jpg hello
bananas
--------------
id user photo comment
32 tom 1.jpg hi
73 bill 5.jpg hello
oranges
--------------
id user photo comment
73 jane 55.jpg wave
22 bill 5.jpg hello
results
-------------
19 tom 1.jpg hi
73 bill 5.jpg hello
46 sam 78.jpg howdy
IO Stats on 4 rows using UNION ALL with newer code
(4 row(s) affected)
Table 'pears'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'fruit'. Scan count 3, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'apples'. Scan count 1, logical reads 16, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'oranges'. Scan count 1, logical reads 115, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
At least there wasn't one physical read which is awesome