我已经从SO中的先前答案中读到,在where语句中使用别名是不可能的。但是我想知道如何重写这个查询?
SELECT
CASE
WHEN app_reports_mgmt.reports.rid = app_reports_mgmt.report_template.rid THEN
app_reports_mgmt.report_template.name
ELSE app_reports_mgmt.reports.name
END AS name
FROM app_reports_mgmt.reports left join app_reports_mgmt.report_template on app_reports_mgmt.reports.rid = app_reports_mgmt.report_template.rid
where LOWER(name) LIKE LOWER('%daily%') and app_reports_mgmt.reports.report_status = 'Active' order by name
答案 0 :(得分:1)
经过一些调查后,问题出在where
子句中,而不是order by
。
您可以通过按位置引用来解决此问题:
select (case when r.rid = t.rid
then t.name
else r.name
end) as name
from app_reports_mgmt.reports r left join
app_reports_mgmt.report_template t
on r.rid = t.rid
where (case when r.rid = t.rid then lower(t.name) else lower(r.name) end) like lower('%daily%') and
r.report_status = 'Active'
order by name;