要在仅位置匹配时返回 ALL,请尝试以下查询
SQL查询
select m.item, m.location,
(CASE WHEN
m.card like t.card THEN m.card
ELSE
'All'
END) as card
from
mst_item as m,
time_factor as t
WHERE
m.location like t.location
甲骨文查询
select mst_item.item, mst_item.location,
(CASE
WHEN
mst_item.card = time_factor.card THEN mst_item.card
ELSE
'All'
END) as card
from
mst_item,
time_factor
WHERE
mst_item.location like time_factor.location;
输出
更好的查询可以是
SQL查询
select m.item, m.location,t.card
from
mst_item as m,
time_factor as t
WHERE
m.location like t.location
甲骨文查询
select mst_item.item, mst_item.location, time_factor.card
from
mst_item,
time_factor
WHERE
mst_item.location like time_factor.location;
输出
另一种变体可以是
select m.item, m.location,
(CASE WHEN
m.card like t.card THEN m.card
WHEN
t.card like 'ALL' THEN t.card
END) as mcard
from
mst_item as m,
time_factor as t
WHERE
m.location like t.location
AND (CASE WHEN
m.card like t.card THEN m.card
WHEN
t.card like 'ALL' THEN t.card
END) != '';
一个变种可以是
SQL查询
select m.item, m.location,
(CASE WHEN
m.card like t.card THEN m.card
WHEN
t.card like 'ALL' THEN t.card
END) as mcard
from
mst_item as m,
time_factor as t
WHERE
m.location like t.location
AND (CASE WHEN
m.card like t.card THEN m.card
WHEN
t.card like 'ALL' THEN t.card
END) != '';
甲骨文查询
select mst_item.item, mst_item.location,
(CASE
WHEN
mst_item.card = time_factor.card THEN mst_item.card
WHEN
time_factor.card like 'ALL' THEN time_factor.card
END) as card
from
mst_item,
time_factor
WHERE
mst_item.location like time_factor.location
AND
(CASE
WHEN
mst_item.card = time_factor.card THEN mst_item.card
WHEN
time_factor.card like 'ALL' THEN time_factor.card
END) IS NOT NULL
;
输出
请检查您想要的输出,以便我们进行相应的修改。
在http://sqlfiddle.com/#!2/53e53/21上的 SQL 小提琴演示
Oracle 小提琴演示在http://sqlfiddle.com/#!4/d28b1/26