是否可以使用自己的元组作为JOIN语句的源?
这是我正在寻找的一个例子:
SELECT u.id, u.name FROM users u
INNER JOIN {{tupl1},{tupel2},{tupel3},...}
答案 0 :(得分:4)
在SQL Server 2008中,您使用Table Value Constructor。
declare @T table(ID int)
insert into @T values (1),(2),(3),(4),(5)
select *
from @T as T1
inner join (
values (1, 'Row 1'),
(2, 'Row 2')
) as T2(ID, Col1)
on T1.ID = T2.ID
答案 1 :(得分:2)
许多dbms平台都支持公用表表达式。
with my_own_tuples as (
select 'value1' as column1, 'value2' as column2
union all
select 'value3', 'value4'
union all
select 'value5', 'value6'
)
select column1, column2
from my_other_table
inner join my_own_tuples on (my_own_tuples.column1 = my_other_table.column1);
答案 2 :(得分:0)
MySql有一些support for Tuples with comparisons and equality,虽然我很难找到明确的参考资料。
例如:
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON (t1.Col1, t1.Col2) = (t2.Col1, t2.Col2);
和
SELECT *
FROM table1 t1
WHERE (t1.Col1, t1.Col2) <= ('SomeValue', 1234);