Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mysql中重用临时表_Mysql_Sql - Fatal编程技术网

在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表可以重用。