[英]CASE Statement in where clause in mysql
I'm trying to fetch data from table where I'm using a CASE condition in the WHERE clause and currently I'm using following query:- 我正在尝试从在WHERE子句中使用CASE条件的表中获取数据,当前正在使用以下查询:
$sQuery = "SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ",
implode(", ", $aColumns))." FROM (select CASE when r.agent_id=24
THEN r.Unit ELSE '--' END AS MyUnit,
CASE when r.agent_id=24 THEN r.landlord_name ELSE '--' END AS landlord_name_new,
r.*,l.loc_name as location,sl.sub_sub_loc as sub_location,
c.category as category,CONCAT(u.first_name, ' ', u.last_name) As agent
from crm_sales r
LEFT JOIN crm_location l ON r.area_location_id=l.loc_id
LEFT JOIN crm_subloc sl ON sl.sub_loc_id = r.sub_area_location_id
LEFT JOIN crm_category c on c.id = r.category_id
LEFT JOIN crm_users u on u.id=r.agent_id
where r.is_active=1 AND r.is_archive=0
AND CASE agent_id WHEN r.agent_id!=24 then r.status=2 else 1=1
group by r.ref) sel
$sWhere
$sOrder
$sLimit
";
Now I want to add one more condition, something like this. 现在,我想再添加一个条件,像这样。
IF(r.agent_id != 24) THEN WHERE r.status=2 IF(r.agent_id!= 24)然后r.status = 2
EDITED: ADD CASE which i want but error 编辑:添加我想但错误的情况
Fix the case/when clause in your WHERE clause to... 将WHERE子句中的case / when子句修复为...
AND CASE WHEN r.agent_id != 24
then r.status = 2
else 1 = 1 end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.