gpt4 book ai didi

mysql - 在 MySQL 中存储配置的最佳方式

转载 作者:行者123 更新时间:2023-11-29 16:14:38 25 4
gpt4 key购买 nike

我有一个场景,我需要在 mysql 表中存储配置,该表具有三列,即 VendorIDServiceIDModeID 。供应商的配置可以通过以下三种最重要的情况来完成。

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

供应商ID、服务ID、模式ID --> 1,NULL,NULL

  • 两列 VendorID、ServiceID 具有非 NULL 值,ModeID 具有 NULL 值。
  • 供应商ID、服务ID、模式ID --> 1,1,NULL

  • 所有三列都具有非 NULL 值。
  • 供应商ID、服务ID、模式ID --> 1,1,1

    当定义了 case 1,2,3 并在 MySQL 查询 WHERE 子句中传递了vendorid、servideid 和 modeid 时,case 3 会覆盖 case 2 和 case 1。

    当未定义情况 3 且定义了情况 1,2 且在 MySQL 查询 WHERE 子句中传递了vendorid、servideid 和 modeid 时,情况 2 会覆盖情况 1。

    当未定义 case 3 和 case 2 且定义了 case 1 且在 MySQL 查询 WHERE 子句中传递了vendorid、servideid 和 modeid 时,则返回 case 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;

    这将返回具有最佳匹配的一行。

    关于mysql - 在 MySQL 中存储配置的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54942582/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com