[英]Mysql: Create inline table within select statement?
Is there a way in MySql to create an inline table to use for join? MySql 中有没有办法创建一个用于连接的内联表?
Something like:就像是:
SELECT LONG [1,2,3] as ID, VARCHAR(1) ['a','b','c'] as CONTENT
that would output那会输出
| ID | CONTENT |
| LONG | VARCHAR(1)|
+------+-----------+
| 1 | 'a' |
| 2 | 'b' |
| 3 | 'c' |
and that I could use in a join like this:我可以在这样的连接中使用:
SELECT
MyTable.*,
MyInlineTable.CONTENT
FROM
MyTable
JOIN
(SELECT LONG [1,2,3] as ID, VARCHAR(1) ['a','b','c'] as CONTENT MyInlineTable)
ON MyTable.ID = MyInlineTable.ID
I realize that I can do我意识到我可以做到
SELECT 1,'a' UNION SELECT 2,'b' UNION SELECT 3,'c'
But that seems pretty evil但这似乎很邪恶
I don't want to do a stored procedure because potentially a,b,c can change at every query and the size of the data as well.我不想执行存储过程,因为 a、b、c 可能会在每次查询和数据大小时发生变化。 Also a stored procedure needs to be saved in the database, and I don't want to have to modify the database just for that.
另外一个存储过程需要保存在数据库中,我不想为此修改数据库。 View is the same thing.
视图是一样的。
What I am really looking for is something that does SELECT 1,'a' UNION SELECT 2,'b' UNION SELECT 3,'c'
with a nicer syntax.我真正想要的是
SELECT 1,'a' UNION SELECT 2,'b' UNION SELECT 3,'c'
语法更好的东西。
The only ways i can remember now is using UNION
or creating a TEMPORARY TABLE
and inserting those values into it.我现在记得的唯一方法是使用
UNION
或创建一个TEMPORARY TABLE
并将这些值插入其中。 Does it suit you?它适合你吗?
TEMPORARY_TABLE
(tested and it works): TEMPORARY_TABLE
(经过测试且有效):
Creation:创建:
CREATE TEMPORARY TABLE MyInlineTable (id LONG, content VARCHAR(1) );
INSERT INTO MyInlineTable VALUES
(1, 'a'),
(2, 'b'),
(3, 'c');
Usage:用法:
SELECT
MyTable.*,
MyInlineTable.CONTENT
FROM
MyTable
JOIN
SELECT * FROM MyInlineTable;
ON MyTable.ID = MyInlineTable.ID
TEMPORARY_TABLES
lifetime (reference) : TEMPORARY_TABLES
生命周期(参考) :
Temporary tables are automatically dropped when they go out of scope, unless they have already been explicitly dropped using DROP TABLE:
临时表在超出范围时会自动删除,除非它们已经使用 DROP TABLE 显式删除:
. .
All other local temporary tables are dropped automatically at the end of the current session.
所有其他本地临时表在当前会话结束时自动删除。
. .
Global temporary tables are automatically dropped when the session that created the table ends and all other tasks have stopped referencing them.
当创建表的会话结束并且所有其他任务停止引用它们时,全局临时表会自动删除。 The association between a task and a table is maintained only for the life of a single Transact-SQL statement.
任务和表之间的关联仅在单个 Transact-SQL 语句的生命周期内维护。 This means that a global temporary table is dropped at the completion of the last Transact-SQL statement that was actively referencing the table when the creating session ended.`
这意味着在创建会话结束时主动引用该表的最后一条 Transact-SQL 语句完成时,将删除全局临时表。`
What I am really looking for is something that does SELECT 1,'a' UNION SELECT 2,'b' UNION SELECT 3,'c' with a nicer syntax.
我真正想要的是 SELECT 1,'a' UNION SELECT 2,'b' UNION SELECT 3,'c' 语法更好的东西。
Yes, it is possible with ROW CONSTRUCTOR introduced in MySQL 8.0.19:是的,在 MySQL 8.0.19 中引入ROW CONSTRUCTOR是可能的:
VALUES ROW (1,'a'), ROW(2,'b'), ROW(3,'c')
and with JOIN:并使用 JOIN:
SELECT *
FROM tab
JOIN (VALUES ROW (1,'a'), ROW(2,'b'), ROW(3,'c') ) sub(id, content)
ON tab.id = sub.id;
Yes.是的。 Do with stored procedures orviews .
使用存储过程或视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.