我试图离开加入两个不同的表,取决于Type
列中的值开头的字符。
例如:
如果列type
的值以's'
=> Left Join table a
开头
如果列type
的值以'g'
=> Left Join table b
开头
我该如何创造这种情况?
您可以在下面通过在ON子句中应用条件来尝试
select * from tablename x
left join tableA y on x.id=y.id and x.type like 's%'
left join tableB z on x.id=z.id and x.type like 'g%'
您可以使用以下解决方案:
SELECT *
FROM table_name t
LEFT JOIN A ON LEFT(t.type, 1) = 's' AND t.id = A.tid
LEFT JOIN B ON LEFT(t.type, 1) = 'g' AND t.id = B.tid