【发布时间】:2017-04-28 06:03:52
【问题描述】:
我正在根据 php 文件中提供的发布文件过滤数据。 我有 10 个输入字段,用户应该填写任意数量的字段,因为它们不是必填字段。 所以我可能会得到 10 个字段中的 10 个,或者也没有。如何使用 mysql 查询编写 where 条件以仅针对已发布的数据字段过滤来自数据库的数据。
<?php
if (!empty($eventInfo))
{
$event = $eventInfo . ',';
}
else
{
$event = NULL;
}
if (!empty($wildInfo))
{
$wild = $wildInfo . ',';
}
else
{
$wild = NULL;
}
if (!empty($additionalInfo))
{
$additional = $additionalInfo . ',';
}
else
{
$additional = NULL;
}
if (!empty($humanInfo))
{
$human = $humanInfo . ',';
}
else
{
$human = NULL;
}
if (!empty($livestockInfo))
{
$livestock = $livestockInfo . ',';
}
else
{
$livestock = NULL;
}
if (!empty($cropInfo))
{
$crop = $cropInfo . ',';
}
else
{
$crop = NULL;
}
if (!empty($propertyInfo))
{
$property = $propertyInfo . ',';
}
else
{
$property = NULL;
}
if (!empty($otherInfo))
{
$other = $otherInfo . ',';
}
else
{
$other = NULL;
}
if (!empty($year))
{
$where = "(EXTRACT(year FROM `e`.event_date_time) = '$year')";
}
else
{
$where = NULL;
}
if (!empty($month))
{
$where1 = "(EXTRACT(month FROM `e`.event_date_time) = '$month')";
}
else
{
$where1 = NULL;
}
if (!empty($animal))
{
$where2 = "`e`.animal_type = '$animal'";
}
else
{
$where2 = NULL;
}
if (!empty($conflictType))
{
$like = '`e`.event_types like %' . $conflictType . '%';
}
$query = "SELECT $event" . "$wild" . "$additional" . "$human" . "$livestock" . "$crop" . "$property" . "$other" . "`d`.`value` as `event_district`, `vdc`.`value` as `event_vdc_munic` FROM `event_info` as `e` LEFT JOIN `wild_animal_info` `wa` ON `e`.`event_id` = `wa`.`event_id` LEFT JOIN `human_victim_info` `hv` ON `e`.`event_id` = `hv`.`event_id` LEFT JOIN `livestock_destruction_info` `lv` ON `e`.`event_id` = `lv`.`event_id` LEFT JOIN `crop_destruction_info` `c` ON `e`.`event_id` = `c`.`event_id` LEFT JOIN `proprty_destruction_info` `p` ON `e`.`event_id` = `p`.`event_id` LEFT JOIN `other_destruction_info` `o` ON `e`.`event_id` = `o`.`event_id` LEFT JOIN `additional_info` `a` ON `e`.`event_id` = `a`.`event_id` LEFT JOIN `district_info` `d` ON `e`.`event_district` = `d`.`id` LEFT JOIN `vdc_munic_info` `vdc` ON `e`.`event_vdc_munic` = `vdc`.`id`";
$select_table = pdo_db()->query($query);
$rows = $select_table->fetch(PDO::FETCH_ASSOC);
现在,如果年份、物种、月份和冲突类型字段中的任何一个为空/非空,我如何在查询末尾添加位置。
【问题讨论】: