如果您只需要在查询中使用一些值,则您实际上不需要创建表或使用CTE。您可以内联它们:
SELECT *
FROM (VALUES(0::INT, -99999::NUMERIC), (1, 100)) AS lookup(key, val)
然后,您可以获得具有的笛卡尔积CROSS JOIN
(其他关系当然可以是正则表,视图等)。例如:
SELECT *
FROM (VALUES(0::int, -99999::numeric), (1, 100)) AS lookup(key, val)
,(VALUES('Red'), ('White'), ('Blue')) AS colors(color);
产生:
key |val |color |
----|-------|------|
0 |-99999 |Red |
1 |100 |Red |
0 |-99999 |White |
1 |100 |White |
0 |-99999 |Blue |
1 |100 |Blue |
或JOIN
具有其他关系的值(也可以是常规表,视图等),例如:
SELECT *
FROM (VALUES(0::int, -99999::numeric), (1, 100)) AS lookup(key, val)
JOIN (VALUES('Red', 1), ('White', 0), ('Blue', 1)) AS colors(color, lookup_key)
ON colors.lookup_key = lookup.key;
产生:
key |val |color |lookup_key |
----|-------|------|-----------|
1 |100 |Red |1 |
0 |-99999 |White |0 |
1 |100 |Blue |1 |