Home > Software design >  SQL Query based on value of another table
SQL Query based on value of another table

Time:02-13

Suppose That I have Table A and Table B as follow. How would I get my expected outcome ? The filtering should be dynamically based on values in Table A as follow

SELECT * FROM TableB
WHERE
(Start = 'A' AND End = 'B')
OR (Start = 'C' AND End = 'D')
OR (Start = 'B' AND End = 'A')

Table A

Start End
A     B
C     D
B     A

Table B

ID Start End  Date
1  A     B    2021-01-01
2  A     B    2021-01-01
3  C     D    2021-01-01
4  B     A    2021-01-01
5  C     D    2021-01-01
6  E     A    2021-01-01
6  D     C    2021-01-01

Expected Result

ID Start End  Date
1  A     B    2021-01-01
2  A     B    2021-01-01
3  C     D    2021-01-01
4  B     A    2021-01-01
5  C     D    2021-01-01

CodePudding user response:

It's seems a simple join is all that is required?

select *
from TableB B
inner join TableA A on
    A.Start = B.Start
    and A.End = B.End
  •  Tags:  
  • sql
  • Related