4

is it possible use own tuples as a source for an JOIN statement?

Here is an example of what I'm looking for:

SELECT u.id, u.name FROM users u INNER JOIN {{tupl1},{tupel2},{tupel3},...}

lszrh
  • 1,531
  • 1
  • 19
  • 28

3 Answers3

4

In SQL Server 2008 you use the 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
Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281
2

Many dbms platforms support common table expressions.

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);

PostgreSQL Common Table Expressions

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
0

MySql has some support for Tuples with comparisons and equality, although I'm struggling to find a definitive reference for this.

For example:

SELECT *
    FROM table1 t1
       INNER JOIN table2 t2
       ON (t1.Col1, t1.Col2) = (t2.Col1, t2.Col2);

And

SELECT *
    FROM table1 t1
       WHERE (t1.Col1, t1.Col2) <= ('SomeValue', 1234);

SqlFiddle showing these here

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285