簡體   English   中英

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

[英]Best way to store configuration in MySQL

我有一種情況,我需要將配置存儲在具有三列(即VendorIDServiceIDModeID)的 mysql表中。 可以通過以下三種主要情況來完成供應商的配置。

  1. 一列VendorID具有非NULL值,其中ServiceID,ModeID具有NULL值。

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

  1. 兩列VendorID,ServiceID具有非NULL值,而ModeID具有NULL值。

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

  1. 所有三列均具有非NULL值。

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

當定義了案例1,2,3並在MySQL查詢WHERE子句中傳遞了vendorid,servideid和modeid時,案例3會覆蓋案例2和案例1。

如果未定義案例3且定義了案例1,2,並且在MySQL查詢WHERE子句中傳遞了vendorid,servideid和modeid,則案例2會覆蓋案例1。

如果未定義案例3和案例2並定義了案例1,並且在MySQL查詢WHERE子句vendorid,servideid和modeid中傳遞,則返回案例1。

現在我的問題是,如何在查詢中一次性傳遞vendorid,servideid和modeid時查詢表以獲取返回的配置,而不必分別查詢3次。

也歡迎任何其他解決上述問題的好的方法。

您可能想要這樣:

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;

這將返回最匹配的一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM