lszrh
lszrh

Reputation: 1582

Use own tuples in JOIN statement

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},...}

Upvotes: 4

Views: 3710

Answers (3)

StuartLC
StuartLC

Reputation: 107347

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

Upvotes: 0

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

Upvotes: 2

Mikael Eriksson
Mikael Eriksson

Reputation: 138990

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

Upvotes: 4

Related Questions