如何将IF语句添加到这样的SQL WHERE条件中?

时间:2018-06-26 15:15:42

标签: mysql sql

我不太喜欢数据库,并且在处理 MySql 查询时遇到以下问题,该查询需要在 WHERE 语句中使用 IF 条件。

我需要这个IF条件,因为where陈述必须与查询本身检索到的国家/地区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_id 值,它执行由localizations_id起作用的第二个选择查询。我正在指定一个检索其country_id的本地化,该本地化用作此where条件的值。很好。

然后,我需要使用检索到的值向 WHERE 条件添加一些 AND 子句。 country_id 只能为1或2。

所以

如果检索到的 country_id 值为1->添加AND条件。

如果检索到的 country_id 值为2->添加另一个ADD条件。

如何解决查询并正确处理这种情况?

2 个答案:

答案 0 :(得分:0)

您可以将内部联接与正确的ON和AND子句配合使用,从而避免

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

答案 1 :(得分:0)

您不需要IF语句,只需要重新考虑一下WHERE子句即可。

由于您希望在LCZ1.country_id为1或2时添加LCZ1.country_id is not null谓词,因此可以重新编写以简单地检查LCZ1.country_id是必需值之一:

AND LCZ1.country_id in (1, 2)