我有一个场景,我想在满足条件时内连接表。我想要类似的东西
SELECT * FROM table_1
CASE
WHEN some_condition THEN INNER JOIN table_2 ON some_other_condition
END
我已经阅读了许多引用资料和之前的 StackOverflow 答案。但是,这些不符合我的要求。
编辑:
Table 1
id | name | price
1 | aaa | 111
2 | bbb | 222
____________________
Table 2
id | discountedPrice | orgId
1 | 100 | org1
2 | 200 | org2
添加了表格。对于某些组织,我想显示唯一的价格。但是,一些用户有价格折扣。因此,想要向少数用户显示折扣。所以,我想有条件地进行内连接。
最佳答案
使用左连接
。您不清楚自己到底想做什么,但类似这样:
select t1.*, coalesce(t2.discounted_price, ?) as discounted_price
from table_1 t1 left join
table_2 t2
on <join conditions> and
<some condition>;
关于mysql - MySql 中 case 条件的内连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54359131/