我需要创建动态INNER JOIN(我相信我还需要)。我有4桌梨,苹果,橘子,香蕉。我在这4个表中使用了所有相同的列名。
另外两张桌子'水果'和'水果评论'
pears apples oranges bananas
-------------------------------------
id
user
photo
comment
date
对于Fruits表,typeid与其他表有关1 =梨,2 =苹果,3 =橙子,4 =香蕉。 Itemid与梨,苹果,橙子或香蕉的id相关。
Fruit
-----------------------------------------------------------------------------
fruitsid
typeid
itemid
我如何从水果表中选择前10名并动态获取相应表格中的匹配行
工作代码
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
旧的非工作代码:
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
)
类似的东西,但我不知道如何正确表达它。下面的预期结果是通过fruitsid DESC从'fruit'表顺序中选择top(3)。
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
使用UNION ALL使用较新代码
的4行上的IO统计信息(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.
至少没有一个物理读物很棒
答案 0 :(得分:0)
这样做的一种方法是使用UNION加入 -
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
)
ORDER BY fruitId DESC
如果您不想包含仅存在Fruits
但不存在于PEAR|APPLES|ORACNGES|BANANAS
中的记录,请改用<{1}}