如何将if语句添加到像这样的sql where条件中?

fjnneemd  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(308)

我不太喜欢db,我在处理mysql查询时遇到了以下问题,需要在where语句中使用if条件。
我需要这个if条件,因为where statment的基必须与查询本身检索到的国家id不同。我试着向你详细解释我需要什么。
这是我的查询(不起作用):

SELECT
    LCZ1.id                                          AS localization_id,
    LCZ1.description                                 AS localization_description, 
    CNT.id                                           AS country_id,
    CNT.country_name                                 AS country_name,
    CNT.isActive                                     AS country_is_active,
    RGN.id                                           AS region_id,
    RGN.region_name                                  AS region_name,
    PRV.id                                           AS province_id,
    PRV.province_name                                AS province_name,
    DST.id                                           AS district_id, 
    DST.district_name                                AS district_name,
    SCT.id                                           AS sector_id,
    SCT.sector_name                                  AS sector_name

FROM Localization                                   AS LCZ1
LEFT JOIN Country                                   AS CNT
     ON LCZ1.country_id = CNT.id
LEFT JOIN Region                                    AS RGN
     ON LCZ1.region_id = RGN.id
LEFT JOIN Province                                  AS PRV
     ON LCZ1.province_id = PRV.id 
LEFT JOIN District                                  AS DST
     ON LCZ1.district_id = DST.id
LEFT JOIN Sector                                    AS SCT
     ON LCZ1.sector_id = SCT.id

WHERE
     (LCZ1.country_id = (SELECT LCZ2.country_id FROM Localization AS LCZ2 WHERE LCZ2.id = 5))

IF(LCZ1.country_id = 1)
BEGIN
AND
     LCZ1.country_id is not null
END

IF(LCZ1.country_id = 2)
BEGIN
AND
     LCZ1.country_id is not null
END

如您所见,where条件从以下内容开始:

WHERE
     (LCZ1.country_id = (SELECT LCZ2.country_id FROM Localization AS LCZ2 WHERE LCZ2.id = 5))

基本上,我选择lcz1.country\u id值执行第二个select查询,该查询由localizations\u id执行。我指定一个localizations检索其country\u id,该id用作此where条件的值。很好用。
然后我需要使用这个检索到的值向where条件添加一些and子句。国家/地区id只能是1或2。
所以,
如果检索到的country\u id值为1-->则添加and条件。
如果检索到的country\u id值为2-->则添加另一个add条件。
如何修复查询并正确处理这种情况?

whlutmcx

whlutmcx1#

您可以使用带有适当的on and and子句的内部连接,以避免where

SELECT  ......

FROM Localization                                   AS LCZ1
LEFT JOIN Country                                   AS CNT
     ON LCZ1.country_id = CNT.id
LEFT JOIN Region                                    AS RGN
     ON LCZ1.region_id = RGN.id
LEFT JOIN Province                                  AS PRV
     ON LCZ1.province_id = PRV.id 
LEFT JOIN District                                  AS DST
     ON LCZ1.district_id = DST.id
LEFT JOIN Sector                                    AS SCT
     ON LCZ1.sector_id = SCT.id

INNER Localization LCZ2 ON LCZ1.country_id = LCZ2.country_id 
         AND LCZ2.id = 5 AND LCZ1.country_id = 1 AND  LCZ1.country_id is not null
a0x5cqrl

a0x5cqrl2#

你不需要 IF 声明,你只需要重新考虑一下 WHERE 一点点。
因为你想添加 LCZ1.country_id is not null predicate 每当lcz1.country\u id为1或2时,您可以将其重新格式化,只需检查lcz1.country\u id是否为所需值之一:

AND LCZ1.country_id in (1, 2)

相关问题