你能定义“字面"吗?SQL中的表?

Can you define quot;literalquot; tables in SQL?(你能定义“字面吗?SQL中的表?)
本文介绍了你能定义“字面"吗?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.

推荐答案

我想你可以用几个 SELECTs 结合 UNIONs 来做一个子查询.

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中的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)