首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否使用php和Mysql显示0实例的空值?

是否使用php和Mysql显示0实例的空值?
EN

Stack Overflow用户
提问于 2018-05-31 17:44:29
回答 1查看 69关注 0票数 -3

如何在$get查询中使用0值而不是null参数。请回答问题。

这是显示所有数据的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();
        }
EN

回答 1

Stack Overflow用户

发布于 2018-05-31 17:59:04

使用**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;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50621301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档