我更改了代码中的查询,因为我希望用户从下拉列表中选择特定值,而不是自由文本搜索。在这种情况下,我必须适应以下事实:如果用户未选择特定值,我仍然希望获得所有相关结果。更改后,查询将返回“无结果”。 我很困惑,因为我已经多次使用过类似的查询。 这是有效的原始查询:
SELECT `companies`.`name`, `companies`.`website`,`country`.`country_name`,
`company_sector`.`notes`, `sector`.`sector_name`,
FORMAT(`financials`.`revenues`,0),
FORMAT(`financials`.`ebitda`,0)
FROM { oj `seta`.`company_sector` AS `company_sector`
RIGHT OUTER JOIN `seta`.`companies` AS `companies` ON `company_sector`.`company_id` = `companies`.`company_id`
LEFT OUTER JOIN `seta`.`country` AS `country` ON `country`.`country_id` = `companies`.`country_id`
LEFT OUTER JOIN `seta`.`financials` AS `financials` ON `financials`.`company_id` = `companies`.`company_id`
LEFT OUTER JOIN `seta`.`contacts` AS `contacts` ON `contacts`.`contact_id` = `companies`.`seta_id`
},
`seta`.`sector` AS `sector`
WHERE
(
(`company_sector`.`sector_id` = `sector`.`sector_id`)
AND
(`companies`.`name` LIKE '%".$query_name."%')
AND
(`company_sector`.`notes` LIKE '%".$query_product."%')
AND
(`country`.`country_name` LIKE '%".$query_country."%')
AND
(`sector`.`sector_name` LIKE '%".$query_sector."%')
这是无效的新查询。如您所见,我仅更改了最后两行。
SELECT `companies`.`name`, `companies`.`website`, `country`.`country_name`,
`company_sector`.`notes`, `sector`.`sector_name`,
FORMAT(`financials`.`revenues`,0),
FORMAT(`financials`.`ebitda`,0)
FROM { oj `seta`.`company_sector` AS `company_sector`
RIGHT OUTER JOIN `seta`.`companies` AS `companies` ON `company_sector`.`company_id` = `companies`.`company_id`
LEFT OUTER JOIN `seta`.`country` AS `country` ON `country`.`country_id` = `companies`.`country_id`
LEFT OUTER JOIN `seta`.`financials` AS `financials` ON `financials`.`company_id` = `companies`.`company_id`
LEFT OUTER JOIN `seta`.`contacts` AS `contacts` ON `contacts`.`contact_id` = `companies`.`seta_id` }, `seta`.`sector` AS `sector`
WHERE
(
(`company_sector`.`sector_id` = `sector`.`sector_id`)
AND
(`companies`.`name` LIKE '%".$query_name."%')
AND
(`company_sector`.`notes` LIKE '%".$query_product."%')
AND
(
(`company_sector`.`sector_id` = ".$query_sector.")
OR
(".$query_sector." IS NULL)
)
)
ORDER BY `financials`.`revenues` ASC
可以帮忙吗? 谢谢 问候 托马索
答案 0 :(得分:1)
中断查询到各节,并在选择过滤器后附加过滤器值。参见下面的示例。
$query = "SELECT `companies`.`name`, `companies`.`website`, `country`.`country_name`, `company_sector`.`notes`, `sector`.`sector_name`, FORMAT(`financials`.`revenues`,0), FORMAT(`financials`.`ebitda`,0) FROM { oj `seta`.`company_sector` AS `company_sector` RIGHT OUTER JOIN `seta`.`companies` AS `companies` ON `company_sector`.`company_id` = `companies`.`company_id` LEFT OUTER JOIN `seta`.`country` AS `country` ON `country`.`country_id` = `companies`.`country_id` LEFT OUTER JOIN `seta`.`financials` AS `financials` ON `financials`.`company_id` = `companies`.`company_id` LEFT OUTER JOIN `seta`.`contacts` AS `contacts` ON `contacts`.`contact_id` = `companies`.`seta_id` }, `seta`.`sector` AS `sector` WHERE
(
(`company_sector`.`sector_id` = `sector`.`sector_id`)
AND
(`companies`.`name` LIKE '%".$query_name."%')
AND
(`company_sector`.`notes` LIKE '%".$query_product."%')";
//check if filter is selected.
if(!empty($query_sector)){
$query .= " AND `company_sector`.`sector_id` = ".$query_sector; //append to main query.
}
$query .= " ORDER BY `financials`.`revenues` ASC";
答案 1 :(得分:0)
您假设一个空字符串与此行匹配IS NULL
:
(".$query_sector." IS NULL)
那将永远无法解决。
您的意思是:
(".$query_selector." = '')