如何创建一个WHERE子句,其中SQL查询中有一些必填字段和一些其他可选字段

时间:2017-11-22 09:07:07

标签: mysql sql

我不是很喜欢数据库而且我有以下疑问。我正在使用 MySql

我有这个返回多个记录的查询(它工作正常),我必须添加 WHERE 子句,使得某些字段是必需的,而其他一些字段是可选的。

所以我有这个问题:

SELECT 
    EnutrifoodMessage.content
    , MessageType.message_type_name
    , Country.country_name
    , IFNULL(Province.province_name, 'All Provinces') as province_name
    , IFNULL(District.district_name, 'Any District') as district_name
    , Crop.crop_name
    , EnutriMessageDetails.creation_date
    , EnutriMessageDetails.message_important_days
    , temp_scale.scale_name as temperature
    , humidity_scale.scale_name as humidity
    , ProcessPhase.phase_name
    , ProcessPhaseAction.process_phase_action_name
    , Urgency.urgency_name as action
    , IFNULL(MeteoWarningDescription.meteo_warning_description_name, '') as emergency
    , IFNULL(EnutriMessageDetails.internal_link, '') as internal_link
    , IFNULL(EnutriMessageDetails.reference_link, '') as reference_link
    , IFNULL(EnutriMessageDetails.external_link, '') as external_link
    , IFNULL(cleared_by_institution.institution_name, '') as message_cleared_by
    , UserType.user_type_name as end_user
    , provider.institution_name as provider
    , ValueAddition.value_addition_name
FROM EnutriMessageDetails
LEFT JOIN EnutrifoodMessage
ON EnutrifoodMessage.id = EnutriMessageDetails.enutri_food_message_id
LEFT JOIN MessageType
ON MessageType.id = EnutriMessageDetails.message_type_id
LEFT JOIN Localization
ON Localization.id = EnutriMessageDetails.localization_id
LEFT JOIN Country
ON Country.id = Localization.country_id
LEFT JOIN Province
ON Province.id = Localization.province_id
LEFT JOIN District
ON District.id = Localization.district_id
LEFT JOIN Crop
ON Crop.id = EnutriMessageDetails.crop_id
LEFT JOIN Scale temp_scale
ON temp_scale.id = EnutriMessageDetails.temp_scale_id
LEFT JOIN Scale humidity_scale
ON humidity_scale.id = EnutriMessageDetails.humidity_scale_id
LEFT JOIN ProcessPhase
ON ProcessPhase.id = EnutriMessageDetails.process_phase_id
LEFT JOIN ProcessPhaseAction
ON ProcessPhaseAction.id = EnutriMessageDetails.process_phase_action_id
LEFT JOIN Urgency
ON Urgency.id = EnutriMessageDetails.urgency_id
LEFT JOIN MeteoWarningDescription
ON MeteoWarningDescription.id = EnutriMessageDetails.meteo_warning_description_id
LEFT JOIN Institution cleared_by_institution
ON cleared_by_institution.id = EnutriMessageDetails.cleared_by_id
LEFT JOIN UserType
ON UserType.id = EnutriMessageDetails.user_type_id
LEFT JOIN Institution provider
ON provider.id = EnutriMessageDetails.provided_by_id
LEFT JOIN ValueAddition
ON ValueAddition.id = EnutriMessageDetails.value_addition_id
WHERE Localization.id = 2
AND Crop.id = 2
ORDER BY EnutrifoodMessage.id

我需要以下列方式添加 WHERE 子句:

1)此字段是必填字段:

Localization.id = 2

UserType.id = 1

2)这些其他字段是可选的:

EnutriMessageDetails.creation_date >= a specified date

Urgency.id = 3

我希望第一种类型的字段必须插入,另一种类型必须是可选的,并且不能填充。

我如何正确处理这种情况?

3 个答案:

答案 0 :(得分:0)

尝试案例表达:

Where  Localization.id = 2 and UserType.id = 1 and 
1 = case when EnutriMessageDetails.creation_date >= **[a specified date]**
              then   case when and Urgency.id = 3
                     then 1
                     else 0
                     end
          else 1
          end

编辑:更正。

答案 1 :(得分:0)

请尝试以下方法测试它是否可行,这是检查它是否为空。那么你就可以做出反应了。

WHERE   Urgency.id LIKE  CASE WHEN Urgency.id IS NULL 
                            THEN Urgency.id
                            ELSE CONCAT('%', Urgency.id, '%')
                            END

我没有针对mySql的测试环境,所以请检查一下它是否可以正常运行。

答案 2 :(得分:0)

似乎你想要:

WHERE (urgency.id = @urgencyid OR @urgencyid IS NULL)

ON (urgency.id = @urgencyid OR @urgencyid IS NULL)

因此,当给出一个ID时,只会选择与该ID匹配的数据,如果没有给出ID,则所有数据都会。

如果表中不存在ID,WHERE子句将不会选择任何记录。所以urgency表应该是内连接的。如果它是外连接的,则WHERE子句将其转换为内连接,因为ID为null的记录将被取消。避免这种伪外连接。

对于内部联接,无论是使用WHERE还是ON都无关紧要。但是,对于外连接,WHERE子句会将其转换为如上所述的内连接,而ON子句仍然是外连接(使用给定ID,可以找到ID或者空虚记录加入)。