2014-10-10 83 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 

你必须使用多个查询。或接受不会使用的数据传输。

此外,你将不得不过滤出你会得到的双重文章