我目前在我的网站上运行此查询,
public function searchCandidates($type=null, $gender=null, $age=null)
{
//die(var_dump($age));
if($age != false) {
$age = implode(", %", $age);
}
$sql = 'SELECT `candidates`.`candidate_id`,
`candidates`.`first_name`,
`candidates`.`surname`,
`candidates`.`gender`,
`candidates`.`DOB`,
`candidates`.`talent`,
`candidates`.`availability`,
`candidates`.`youtube_showreel_1`,
`candidates`.`youtube_showreel_desc_1`,
`candidates`.`date_created`,
`candidates`.`new_talent`,
DATE_FORMAT(NOW(), "%Y") - DATE_FORMAT(`candidates`.`DOB`, "%Y") - (DATE_FORMAT(NOW(), "00-%m-%d") < DATE_FORMAT(`candidates`.`DOB`, "00-%m-%d")) as `age`,
`candidate_assets`.`url`,
`candidate_assets`.`asset_size`,
`candidate_assets`.`weight`
FROM `candidates`
LEFT JOIN `candidate_assets` ON `candidate_assets`.`candidates_candidate_id` = `candidates`.`candidate_id`
WHERE `candidates`.`show_on_site` = "urban talent" AND `candidates`.`visible` = "yes" AND `candidate_assets`.`weight` = 1';
//is there a certain criteria
if($type != "0") { $sql .= ' AND `candidates`.`talent` LIKE "%'.$type.'%"'; }
if($age != false) { $sql .= ' AND `candidates`.`playing_age` LIKE "%'.$age.'%"';}
if($gender != false){ $sql .= ' AND `candidates`.`gender` = "'.$gender.'"'; }
$sql .= ' GROUP BY `candidates`.`candidate_id` ORDER BY `candidates`.`surname` ASC, `candidate_assets`.`weight` ASC';
$query = $this->db->query($sql);
//die(print_r($query->result_array()));
//echo $this->db->last_query();
//die();
return $query->result_array();
}
我正在努力让这个查询按照我的意愿运行,我想显示所有结果,无论用户是否有图像(candidate_assets
。url
),但是如果他们确实有然后我只需要显示权重为1的图像。目前,我的查询仅返回具有加权为1的图像的用户。
我想要的是支持所有用户,如果他们有图像(或多个图像),则返回权重为1的图像。
我做错了什么?
答案 0 :(得分:1)
如果使用where子句中第二个表中的列,则LEFT JOIN没有任何区别。在where子句中允许它们为NULL,或者将它们移动到ON子句:
你有:
LEFT JOIN `candidate_assets`
ON `candidate_assets`.`candidates_candidate_id` = `candidates`.`candidate_id`
WHERE `candidates`.`show_on_site` = "urban talent"
AND `candidates`.`visible` = "yes"
AND `candidate_assets`.`weight` = 1';
制作:
LEFT JOIN `candidate_assets`
ON `candidate_assets`.`candidates_candidate_id` = `candidates`.`candidate_id`
AND `candidate_assets`.`weight` = 1 /*moved here*/
WHERE `candidates`.`show_on_site` = "urban talent"
AND `candidates`.`visible` = "yes"
或者可能(不那么“合乎逻辑”)
LEFT JOIN `candidate_assets`
ON `candidate_assets`.`candidates_candidate_id` = `candidates`.`candidate_id`
WHERE `candidates`.`show_on_site` = "urban talent"
AND `candidates`.`visible` = "yes"
AND (`candidate_assets`.`weight` = 1 OR `candidate_assets`.`weight` IS NULL)
现在您将拥有所需的所有行。要有选择地只选择一个图像(如果有图像),请让您的应用程序代码处理该图像,或者使用IF查询稍微调整您的SELECT子句。
答案 1 :(得分:0)
试试这个 -
$sql = 'SELECT `candidates`.`candidate_id`,
`candidates`.`first_name`,
`candidates`.`surname`,
`candidates`.`gender`,
`candidates`.`DOB`,
`candidates`.`talent`,
`candidates`.`availability`,
`candidates`.`youtube_showreel_1`,
`candidates`.`youtube_showreel_desc_1`,
`candidates`.`date_created`,
`candidates`.`new_talent`,
DATE_FORMAT(NOW(), "%Y") - DATE_FORMAT(`candidates`.`DOB`, "%Y") - (DATE_FORMAT(NOW(), "00-%m-%d") < DATE_FORMAT(`candidates`.`DOB`, "00-%m-%d")) as `age`,
`candidate_assets`.`url`,
`candidate_assets`.`asset_size`,
IF(`candidate_assets`.`url` IS NULL, 0, 1) AS `weight`
FROM `candidates`
LEFT JOIN `candidate_assets` ON `candidate_assets`.`candidates_candidate_id` = `candidates`.`candidate_id`
WHERE `candidates`.`show_on_site` = "urban talent" AND `candidates`.`visible` = "yes"';