如何在查询模型中使用 if 语句
How to use if statement in query model
尝试搜索但没有找到解决我问题的答案。我想在此查询中包含一个 if 条件 select:
$this->datatables->select('id,username,password,email,is_active,is_role,created_at,updated_at');
$this->datatables->add_column('action', anchor(site_url('ms_users/read/'), 'Read') . " | " . anchor(site_url('ms_users/update/'), 'Update') . " | " . anchor(site_url('ms_users/delete/'), 'Delete', 'onclick="javasciprt: return confirm(\'Are You Sure ?\')"'), 'id');
return $this->datatables->generate();
我想为 is_active 和 is_role 做这样的:
/*
if (is_active == 1) {
echo "Active";
} else {
echo "Nonaktif";
}
*/
/*
if (is_role == 1) {
echo "Admin";
} else if (is_role == 2){
echo "Users";
}
*/
可能是这样
$this->datatables->select('id,username,password,email,if(is_active=1,"Active","Nonaktif") as is_active,if(is_role=1,"Admin",if(is_role=2,"Users","Other users")) as is_role,created_at,updated_at');
或
尝试对 2 个以上的角色使用 CASE 语句
$this->datatables->select('id,username,password,email,if(is_active=1,'Active','Nonaktif') as is_active,CASE
WHEN is_role = 1 THEN "Admin"
WHEN is_role = 2 THEN "Users"
ELSE "Other User"
END AS is_role,created_at,updated_at');
尝试搜索但没有找到解决我问题的答案。我想在此查询中包含一个 if 条件 select:
$this->datatables->select('id,username,password,email,is_active,is_role,created_at,updated_at');
$this->datatables->add_column('action', anchor(site_url('ms_users/read/'), 'Read') . " | " . anchor(site_url('ms_users/update/'), 'Update') . " | " . anchor(site_url('ms_users/delete/'), 'Delete', 'onclick="javasciprt: return confirm(\'Are You Sure ?\')"'), 'id');
return $this->datatables->generate();
我想为 is_active 和 is_role 做这样的:
/*
if (is_active == 1) {
echo "Active";
} else {
echo "Nonaktif";
}
*/
/*
if (is_role == 1) {
echo "Admin";
} else if (is_role == 2){
echo "Users";
}
*/
可能是这样
$this->datatables->select('id,username,password,email,if(is_active=1,"Active","Nonaktif") as is_active,if(is_role=1,"Admin",if(is_role=2,"Users","Other users")) as is_role,created_at,updated_at');
或
尝试对 2 个以上的角色使用 CASE 语句
$this->datatables->select('id,username,password,email,if(is_active=1,'Active','Nonaktif') as is_active,CASE
WHEN is_role = 1 THEN "Admin"
WHEN is_role = 2 THEN "Users"
ELSE "Other User"
END AS is_role,created_at,updated_at');