在mysql中重用临时表
我有这样一个问题:在mysql中重用临时表,mysql,sql,Mysql,Sql,我有这样一个问题: (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)
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)
如何重新使用而不是重新查询此临时表
从文章中选择id、content、userid,其中type='A'LIMIT 10
?我将为要重新使用的语句定义一个视图,并给它一个适当的名称。此排序实现了您的要求
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
你必须使用多个查询。或者接受不使用的数据传输
此外,您还必须过滤掉将要返回的重复文章如何重用?你想做什么?@juergend我只是觉得有比重新查询这个tmp表更好的解决方案。也许吧。但我们仍然不知道您到底想做什么。@juergend实际上,我正在尝试将我的两个查询与
union
合并。只是因为,我觉得这个tmp表可以重用。