Conditional Inner Join

后端 未结 4 679
醉梦人生
醉梦人生 2021-02-05 05:12

I want to be able to inner join two tables based on the result of an expression.

What I\'ve been trying so far:

INNER JOIN CASE WHEN Reg         


        
4条回答
  •  生来不讨喜
    2021-02-05 05:56

    You probably need to perform two left joins, one onto TimeRegistration and one onto DrivingRegistration, and return the fields you want from the appropriate join table something like this:

    LEFT JOIN TimeRegistration ON TimeRegistration.RegistreringsId = R.Id
    LEFT JOIN DrivingRegistration ON DrivingRegistration.RegistreringsId = R.Id
    

    and you select statement would be something like this:

    SELECT CASE WHEN RegT.Type = 1 THEN TimeRegistration.Foo ELSE DrivingRegistration.Bar END
    

    I like what you're trying to do, but I don't think SQL is that clever.

提交回复
热议问题