如何解决和编写此查询?

vwhgwdsa  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(295)

我有三张这样的table:
表1有1行数字:

123
124
125
126
127

等。
表2与表1类似,但包含更多元素:

123
124
125
158
152
175

等。
主表有许多行,我在这里挣扎:

select
   a,
   b,
   c,
   if HIT in Table1 and NO HIT in Table2 then '1G'
   else if HIT in Table1 AND HIT in Table2 then '2G'
   else d,
   e,
   f,
   g,
   etc
from Table3

解释得有点混乱,但我不知道该怎么说。。。对不起的!
希望你们能帮我解决这个问题!
-盖尔a。

zour9fqk

zour9fqk1#

你可以用两个 LEFT JOIN . 例如:

select
  a,
  b,
  c,
  case when b.col is not null and c.col is null then '1G'
       when b.col is not null and c.col is not null then '2G'
       else d end,
  e,
  f,
  g,
  etc
from table3 a
left join table1 b on b.col = a.col
left join table2 c on c.col = a.col

相关问题