Skip to content

Instantly share code, notes, and snippets.

@randika
Last active December 12, 2015 06:29
Show Gist options
  • Save randika/4729773 to your computer and use it in GitHub Desktop.
Save randika/4729773 to your computer and use it in GitHub Desktop.
SQL SELECT WHERE IN CLAUSE with multiple columns
Select DISTINCT t1.* from table1 t1 INNER JOIN(
SELECT table2_col_1 ,table2_col_2, table2_col_3 from table2 t2
) temp_table
ON t1.Id = temp_table.table2_col_1 OR t1.id = temp_table.table2_col_2 OR t1.id = temp_table.table2_col_3
Select DISTINCT t.* from topics t INNER JOIN(
SELECT topic_one ,topic_two, topic_three from entries e WHERE e.politician_id = 3
) temp_table
ON t.Id = temp_table.topic_one OR t.id = temp_table.topic_two OR t.id = temp_table.topic_three
-- Another example
-- ===============
select distinct topic_one as `topic_id`,t.title from entries e1
inner join topics t on e1.topic_one = t.id where politician_id=3 and topic_one IS not null
union
select distinct topic_two as `topic_id`,t.title from entries e2
inner join topics t on e2.topic_two = t.id where politician_id=3 and topic_two IS not null
union
select distinct topic_three as `topic_id`,t.title from entries e3
inner join topics t on e3.topic_three = t.id where politician_id=3 and topic_three IS not null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment