从mysql中的值中选择

cri*_*stm 15 mysql postgresql select

从值中选择MySQL的方式是什么?

select c from (values (1), (2), (3)) as t(c);
Run Code Online (Sandbox Code Playgroud)

我们的想法是能够做到这样的事情:

select * from table, (values (1), (2), (3)) as temp(c) where ...;
Run Code Online (Sandbox Code Playgroud)

作为参考,这里是Postgres文档:http: //www.postgresql.org/docs/9.1/static/sql-values.html

a1e*_*x07 15

从您提供的链接:

VALUES(1,'one'),(2,'two'),(3,'three');
这将返回一个包含两列和三行的表.它实际上相当于:
SELECT 1 AS column1,'one'AS column2
UNION ALL
SELECT 2,'
two'UNION ALL
SELECT 3,'three';

所以你需要

select * from 
table1,
(
   SELECT 1 AS val
   UNION ALL
   SELECT 2
   UNION ALL 
   SELECT 3 
)b
Run Code Online (Sandbox Code Playgroud)

  • 这与我的示例等效,但是与Postgres语法相比似乎需要大量工作。MySQL有这样的选择吗? (2认同)

Hob*_* C. 9

在 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 |
+----------+----------+----------+
Run Code Online (Sandbox Code Playgroud)


Bri*_*n D 6

这是解决缺乏WITH支持的另一种方法MySQL

create temporary table tmp (c int);

insert into tmp (c)
values (1), (2), (3);

select * from tmp;
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的回答,这太伤心了。 (9认同)