我有一个场景,需要将配置存储在mysql表中,其中包含三个列,即VendorID、ServiceID和ModeID。供应商的配置可以通过以下三种覆盖情况来完成。
VendorID,ServiceID,ModeID -> 1,NULL,NULL
VendorID,ServiceID,ModeID -> 1,1,NULL
VendorID,ServiceID,ModeID -> 1,1,1
当定义了case 1、2、3并在MySQL查询中传递了子句vendorid、servideid和modeid时,那么case 3将覆盖case 2和case 1。
如果没有定义案例3,定义了案例1,2,并且在MySQL查询中传递了子句vendorid、servideid和modeid,那么案例2将覆盖案例1。
如果没有定义案例3和案例2,并且定义了案例1,并且在MySQL查询中传递子句vendorid、servideid和modeid,则返回案例1。
现在我的问题是,当供应商in、servideid和modeid在查询中一次传递时,如何查询表以获得返回的配置,而不必分别查询3次。
任何其他解决上述问题的好办法也是值得欢迎的。
发布于 2019-03-01 11:47:09
你可能想要这个:
where (VendorID, ServiceID, ModelID) = ($VendorID, $ServiceID, $ModelID) or
( (VendorID, ServiceID) = ($VendorID, $ServiceID) and ModelId is null) or
( VendorID = $VendorID and ServiceID is null and ModelId is null)或者,您可能希望:
select t.*
from t
where VendorId = $VendorId and
(ServiceId = $ServiceId or ServiceId is null) and
(ModelId = $ModelId or ModelId is null)
order by ( ServiceId is not null ) desc,
( ModelId is not null ) desc
limit 1;这将返回一行与最佳匹配。
https://stackoverflow.com/questions/54942582
复制相似问题