postgresql 从mysql中的值中选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9216021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
select from values in mysql
提问by crististm
What would be the MySQL way for doing a select from values?
从值中进行选择的 MySQL 方式是什么?
select c from (values (1), (2), (3)) as t(c);
The idea is to be able to do something like this:
这个想法是能够做这样的事情:
select * from table, (values (1), (2), (3)) as temp(c) where ...;
For reference, here is the Postgres doc: http://www.postgresql.org/docs/9.1/static/sql-values.html
作为参考,这里是 Postgres 文档:http: //www.postgresql.org/docs/9.1/static/sql-values.html
回答by a1ex07
From the link you provided :
从您提供的链接:
VALUES (1, 'one'), (2, 'two'), (3, 'three');
This will return a table of two columns and three rows. It's effectively equivalent to:
SELECT 1 AS column1, 'one' AS column2
UNION ALL
SELECT 2, 'two'
UNION ALL
SELECT 3, 'three';
值 (1, '一'), (2, '二'), (3, '三');
这将返回一个两列三行的表格。它实际上等效于:
SELECT 1 AS column1, 'one' AS column2
UNION ALL
SELECT 2, 'two'
UNION ALL
SELECT 3, 'three';
So you need
所以你需要
select * from
table1,
(
SELECT 1 AS val
UNION ALL
SELECT 2
UNION ALL
SELECT 3
)b
回答by Brian D
This is another way to get around the lack of WITH
support in MySQL
:
这是解决缺乏WITH
支持的另一种方法MySQL
:
create temporary table tmp (c int);
insert into tmp (c)
values (1), (2), (3);
select * from tmp;
回答by C.Hobin
In MySQL 8.0.19, this syntax is supported. please refer to this official link
在 MySQL 8.0.19 中,支持此语法。请参考这个官方链接
mysql> VALUES ROW(1,-2,3), ROW(5,7,9), ROW(4,6,8);
+----------+----------+----------+
| column_0 | column_1 | column_2 |
+----------+----------+----------+
| 1 | -2 | 3 |
| 5 | 7 | 9 |
| 4 | 6 | 8 |
+----------+----------+----------+