如果您执行此查询
SELECT 'test-a1' AS name1, 'test-a2' AS name2
Run Code Online (Sandbox Code Playgroud)
结果将是一行选择,其中两列具有以下值:
test-a1, test-a2
Run Code Online (Sandbox Code Playgroud)
如何修改上述查询以选择多行,例如
test-a1, test-a2
test-b1, test-b2
test-c1, test-c2
Run Code Online (Sandbox Code Playgroud)
我知道如何用UNION做到这一点,但我觉得有一种更简单的方法可以做到这一点.
PS.很抱歉这样一个基本问题,谷歌很难.
小智 41
值关键字可以使用如下.
select * from
(values ('test-a1', 'test-a2'), ('test-b1', 'test-b2'), ('test-c1', 'test-c2')) x(col1, col2)
Run Code Online (Sandbox Code Playgroud)
ope*_*hac 37
SELECT 'test-a1' AS name1, 'test-a2' AS name2
UNION ALL
SELECT 'test-b1', 'test-b2'
UNION ALL
SELECT 'test-c1', 'test-c2'
Run Code Online (Sandbox Code Playgroud)
扩展@openshac对oracle 的答案,因为下面提到的代码适用于oracle:
SELECT 'test-a1' AS name1, 'test-a2' AS name2 from dual
UNION ALL
SELECT 'test-b1', 'test-b2' from dual
UNION ALL
SELECT 'test-c1', 'test-c2' from dual
Run Code Online (Sandbox Code Playgroud)
小智 5
以下代码在 MSSQL 环境中适用于我:
SELECT Name1,Name2 FROM(VALUES ('test-a1', 'test-a2'),
('test-b1', 'test-b2'),
('test-c1', 'test-c2'))AS Test(Name1,Name2)
Run Code Online (Sandbox Code Playgroud)
输出:
Name1 Name2
------- -------
test-a1 test-a2
test-b1 test-b2
test-c1 test-c2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47865 次 |
| 最近记录: |