2014-10-10 46 views
0

我有這樣的查詢:使用這個臨時表中的MySQL

(SELECT id,content,userid FROM article WHERE type='A' LIMIT 10) 
UNION ALL 
(SELECT c.id,c.content,c.userid FROM comment AS c 
JOIN (SELECT id,content,userid FROM article WHERE type='A' LIMIT 10) AS a 
ON c.articleid = a.id) 

如何重新使用,而不是重新查詢該臨時表SELECT id,content,userid FROM article WHERE type='A' LIMIT 10

+0

重用如何?您想做什麼? – 2014-10-10 08:17:07

+0

@juergend我只是覺得可能比重新查詢這個tmp表更好。 – user3925697 2014-10-10 08:22:52

+0

也許吧。但我們仍然不知道你想要做什麼。 – 2014-10-10 08:25:20

回答

0

我將爲要重用的語句定義一個視圖,併爲其指定一個適當的名稱。

0

這種排序實現了你所要求的。

SELECT 
    c.id,c.content,c.userid, 
    a.id,a.content,a.userid 
FROM comment AS c 
    JOIN article AS a ON c.articleid = a.id 
WHERE a.type='A' 

# This will limit #comments + #articles, not usable 
# Removing it means a lot of useless data being transferred 
LIMIT 10 

你必須使用多個查詢。或接受不會使用的數據傳輸。

此外,你將不得不過濾出你會得到的雙重文章