是否有任何SQL子查询语法可让您从字面上定义临时表?
例如,类似
SELECT MAX(count) AS max, COUNT(*) AS count FROM ( (1 AS id, 7 AS count), (2, 6), (3, 13), (4, 12), (5, 9) ) AS mytable INNER JOIN someothertable ON someothertable.id=mytable.id
这样可以省去执行两个或三个查询的步骤:创建临时表,将数据放入其中,然后在联接中使用它。
我正在使用MySQL,但会对其他可以做类似事情的数据库感兴趣。
我想您可以将几个SELECTs与UNIONs 组合在一起进行子查询。
SELECT
UNION
SELECT a, b, c, d FROM ( SELECT 1 AS a, 2 AS b, 3 AS c, 4 AS d UNION ALL SELECT 5 , 6, 7, 8 ) AS temp;