简体   繁体   English

在MySQL中存储配置的最佳方法

[英]Best way to store configuration in MySQL

I have a scenario where i need to store configurations in mysql table having three columns namely VendorID , ServiceID and ModeID . 我有一种情况,我需要将配置存储在具有三列(即VendorIDServiceIDModeID)的 mysql表中。 Configuration for a vendor can be done with three overriding cases as follows. 可以通过以下三种主要情况来完成供应商的配置。

  1. One column VendorID having non-NULL value with ServiceID ,ModeID having NULL value. 一列VendorID具有非NULL值,其中ServiceID,ModeID具有NULL值。

VendorID,ServiceID,ModeID -- > 1,NULL,NULL VendorID,ServiceID,ModeID-> 1,NULL,NULL

  1. Two columns VendorID,ServiceID having non-NULL values with ModeID having NULL value. 两列VendorID,ServiceID具有非NULL值,而ModeID具有NULL值。

VendorID,ServiceID,ModeID -- > 1,1,NULL VendorID,ServiceID,ModeID-> 1,1,NULL

  1. All three columns having non NULL values. 所有三列均具有非NULL值。

VendorID,ServiceID,ModeID -- > 1,1,1 VendorID,ServiceID,ModeID-> 1,1,1

When case 1,2,3 are defined and in the MySQL query WHERE clause vendorid,servideid and modeid are passed, then case 3 overrides case 2 and case 1. 当定义了案例1,2,3并在MySQL查询WHERE子句中传递了vendorid,servideid和modeid时,案例3会覆盖案例2和案例1。

When case 3 is not defined and case 1,2 are defined and in the MySQL query WHERE clause vendorid,servideid and modeid are passed, then case 2 overrides case 1. 如果未定义案例3且定义了案例1,2,并且在MySQL查询WHERE子句中传递了vendorid,servideid和modeid,则案例2会覆盖案例1。

When case 3 and case 2 are not defined and case 1 is defined and in the MySQL query WHERE clause vendorid,servideid and modeid are passed, then case 1 is returned. 如果未定义案例3和案例2并定义了案例1,并且在MySQL查询WHERE子句vendorid,servideid和modeid中传递,则返回案例1。

Now my question is, how can i query the table to get the configuration returned when vendorid,servideid and modeid are passed in the query in one go without having to query 3 times separately. 现在我的问题是,如何在查询中一次性传递vendorid,servideid和modeid时查询表以获取返回的配置,而不必分别查询3次。

Any other good approach for the above problem is also welcome. 也欢迎任何其他解决上述问题的好的方法。

You may want this: 您可能想要这样:

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)

Alternatively you may want: 或者,您可能想要:

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;

This returns one row with the best match. 这将返回最匹配的一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM