在SQL语句的声明中,我根据用户的输入实现了if / else语句。这句话:
if($form['filter']['date'] && $form['filter']['dateTill'] == ""){
$sql .= '(select results.completed from results where results.client_id = c.id and results.completed IS NOT NULL AND results.completed > "'.$form['filter']['date'].'" order by results.id desc limit 1) AS last_completed_analysis, ';
} else if($form['filter']['dateTill'] && $form['filter']['dateTill'] == "") {
$sql .= '(select results.completed from results where results.client_id = c.id and results.completed IS NOT NULL AND results.completed < "'.$form['filter']['dateTill'].'" order by results.id desc limit 1) AS last_completed_analysis, ';
} else if($form['filter']['dateTill'] && $form['filter']['dateTill']){
$sql .= '(select results.completed from results where results.client_id = c.id and results.completed IS NOT NULL AND results.completed > "'.$form['filter']['date'].'" AND results.completed <"'.$form['filter']['dateTill'].'" order by results.id desc limit 1) AS last_completed_analysis, ';
}
else {
$sql .= '(select results.completed from results where results.client_id = c.id and results.completed IS NOT NULL order by results.id desc limit 1) AS last_completed_analysis, ';
}
所有这些都是last_completed_analysis
别名。但是,is not null
不起作用,因此我想声明一个带有is not null statement
的where子句,如:
WHERE last_completed_analysis IS NOT NULL
然而..这不起作用。我可以复制粘贴last_completed_analysis
的整个声明,但这段代码对我来说太长了,我知道它可以做得更好......有人可以帮我宣布这个where子句吗?
答案 0 :(得分:3)
您不能在where condition
中使用别名您可以按顺序使用别名,也可以在分组中的最新版本中使用,但不能在..
中使用这是因为以特定顺序评估了列区域,并且在评估where条件时,别名(目前)尚未解析
所以你应该使用完整的代码,例如:
WHERE (select results.completed
from results
where results.client_id = c.id
and results.completed
IS NOT NULL order by results.id desc limit 1) IS NOT NULL
并小心使用基于字符串连接的非用户输入..