您能定义“文字"字样吗? SQL中的表格? [英] Can you define "literal" tables in SQL?

查看:67
本文介绍了您能定义“文字"字样吗? SQL中的表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何SQL子查询语法可让您从字面上定义临时表?

Is there any SQL subquery syntax that lets you define, literally, a temporary table?

例如,类似

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

这样可以省去两个或三个查询:创建临时表,将数据放入其中,然后在联接中使用它.

This would save having to do two or three queries: creating temporary table, putting data in it, then using it in a join.

我正在使用MySQL,但会对其他可以做类似事情的数据库感兴趣.

I am using MySQL but would be interested in other databases that could do something like that.

推荐答案

我想您可以用多个SELECTUNION组合来进行子查询.

I suppose you could do a subquery with several SELECTs combined with UNIONs.

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;

这篇关于您能定义“文字"字样吗? SQL中的表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆