如果主条件匹配,返回结果基于主条件,无论二级条件是否匹配,如果没有匹配结果,则返回二级条件的数据库。mysql初选条件
以交换外汇为例,我有一个汇率表,我需要拿到美元汇率:英镑,如果没有,那么得到英镑汇率:美元。如果两者都有,只返还美元:英镑。
id origin_currency target_currency rate_from rate_to
1 1 2 1 0.75
2 2 1 1 1.34
对于上面的数据,查询sql应该只返回第一条记录。
我可以用下面的SQL来得到我想要的:
select * from exchange_rate
where origin_currency = 1 and target_currency = 2
union
select * from exchange_rate
where origin_currency = 2 and target_currency = 1
limit 1
然而,还有没有其他更好的解决办法?
你怎么知道哪个记录是美元到英镑,反之亦然?我想我一直在看你为什么有外汇数据,其中一对货币的两种兑换率互不相反。你打算如何使用这些数据? –