从值中进行选择的MySQL方法是什么?
select c from (values (1), (2), (3)) as t(c);
我的想法是能够做到这样的事情:
select * from table, (values (1), (2), (3)) as temp(c) where ...;
供参考,这是Postgres文档: http://www.postgresql.org/docs/9.1/static/sql-values.html
答案 0 :(得分:14)
从您提供的链接:
VALUES(1,'one'),(2,'two'),(3,'three');
这将返回一个包含两列和三行的表。它实际上相当于:
SELECT 1 AS column1,'one'AS column2
UNION ALL
选择2,'两个' UNION ALL
选择3,'三';
所以你需要
select * from
table1,
(
SELECT 1 AS val
UNION ALL
SELECT 2
UNION ALL
SELECT 3
)b
答案 1 :(得分:5)
在MySQL 8.0.19中,支持此语法。请参阅此official link
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 |
+----------+----------+----------+
答案 2 :(得分:3)
这是解决WITH
中缺少MySQL
支持的另一种方式:
create temporary table tmp (c int);
insert into tmp (c)
values (1), (2), (3);
select * from tmp;