如何在null
查询中取0值而不是$get
参数,其中persent是alise。请回答这个问题。
这是显示所有数据的api函数。
public function get_vote_list($parameter) {
$data_available = "0";
$access_token = $parameter->token;
$type = $parameter->election_type;
$limit = $parameter->limit;
$offset = $parameter->page;
$offset = $limit * $offset;
$test=null;
$check_feature_type = $this->common->get_feature_type('Election');
$check_total_hospitals = $this->common->get_count_election();
$user_id = $this->common->get_access_token_id($access_token);
if ($check_total_hospitals > 0) {
$reset_read_count = "update tbl_feature_read_count set read_count = '0', modified_date =now() where user_id = '$user_id' and feature_id = '".$check_feature_type->feature_id."'";
$this->db->query($reset_read_count);
}
$type_where = " ";
switch ($type) {
case "mla": $type_where = " where election_type='1' ";
break;
case "nagarsevak": $type_where = " where election_type='2' ";
break;
default:
$type_where = " where election_type in ('1','2') ";
break;
}
$where_election_type_id = " where election_type='".$type."' ";
$get = "SELECT id,candidate_name,image,electionDate,election_type,education,description,ward,election_name,location,FROM_UNIXTIME(createdOn, '%e %b %Y') as createdOn ,(SELECT COUNT(id) AS `count` FROM tbl_election_support where election_id =tbl_elections.id) voteCount ,(select count(at.id) from tbl_elections as at where at.election_type=tbl_elections.election_type) as candidate_count,(SELECT COUNT(id) AS `count` FROM tbl_election_support where election_id in (select at.id from tbl_elections as at where at.election_type=tbl_elections.election_type)) total_vote_count,ROUND((SELECT COUNT(id) AS `count` FROM tbl_election_support where election_id =tbl_elections.id)/(SELECT COUNT(id) AS `count` FROM tbl_election_support where election_id in (select at.id from tbl_elections as at where at.election_type=tbl_elections.election_type))*100, 2) persent from tbl_elections $where_election_type_id
ORDER BY ward DESC";
$word_get = " SELECT * from tbl_election_ward $where_election_type_id limit ".$offset.",".$limit."";
$list = $this->db->query($get);
$list1 = $list->fetch_all(MYSQLI_ASSOC);
$test = NULL;
$word_get1 = $this->db->query($word_get);
$word_get2 = $word_get1->fetch_all(MYSQLI_ASSOC);
foreach($list1 as $row){
//-------start to fetch food total views
$view_query="SELECT COUNT(user_id) AS support FROM tbl_election_support";
$view_query_result=$this->db->query($view_query);
$view_data = $view_query_result->fetch_assoc();
}
foreach ($word_get2 as $key => $value) {
$test[$value['id']]=$value;
}
foreach ($list1 as $key => $value) {
//$test['ward'][$value['ward']]['wardlist'][]=$value;
$test[$value['ward']]['wardlist'][]=$value;
}
$value1=null ;
foreach ($word_get2 as $key => $value) {
$value1[]=$test[$value['id']];
}
$test=$value1;
date_default_timezone_set("Asia/Kolkata");
return array("status" => TRUE, "message" => "success", "data" => $test, "extra_detail" => array("curdate" => date("d M Y"), "data_available" => $data_available));
if($token != "0" && $data){
return array("status" => TRUE, "message" => "success", "data" => $test, "extra_detail" => array("curdate" => date("d M Y"), "data_available" => $data_available));
}
else
if(!$data){
return array("status" => TRUE, "message" => "No comments", "data" => $data, "extra_detail" => array("curdate" => date("d M Y"), "data_available" => $data_available));
}
else
{
return array("status" => FALSE, "message" => "invalid token", "extra_detail" => array("curdate" => date("d M Y"), "data_available" => '0'));
}
exit();
}
答案 0 :(得分:1)
使用** coalesce**
函数来避免空值
SELECT id,candidate_name,image,electionDate,election_type,education,
description,ward,election_name,location,FROM_UNIXTIME(createdOn, '%e %b %Y') as createdOn ,
(SELECT COUNT(id) AS `count`
FROM tbl_election_support
where election_id =tbl_elections.id
) voteCount ,
(select count(at.id)
from tbl_elections as at
where at.election_type=tbl_elections.election_type
) as candidate_count,
(SELECT COUNT(id) AS `count`
FROM tbl_election_support
where election_id in (select at.id
from tbl_elections as at
where at.election_type=tbl_elections.election_type
)
) total_vote_count,
coalesce(ROUND(
(SELECT COUNT(id) AS `count`
FROM tbl_election_support
where election_id =tbl_elections.id
)/
(SELECT COUNT(id) AS `count`
FROM tbl_election_support
where election_id in (select at.id
from tbl_elections as at
where at.election_type=tbl_elections.election_type
)
)*100, 2
),0) persent
from tbl_elections
$where_election_type_id
ORDER BY ward DESC;