簡體   English   中英

我有兩個表第一個表有一些數據另一個表第二個有所有數據現在聯接查詢空值顯示在json響應中

[英]i have two tables 1st table have some data another table 2nd have all data now join query null value showing in json response

我有兩個表第一個表有一些數據另一個表第二個現在所有數據都離開了聯接查詢放在json響應中顯示空值如何替換空值我正在使用郵遞員,我在json字段中比較新鮮。

如果存在數據,那么數據將像這些字段那樣顯示

“athlete_attendance_id”: “48”, “coach_id”: “302”, “athlete_id”: “380”, “athlete_attendance”: “1”

{"responseCode":200,"responseMessage":"Athlete details Successfully display","data":[{"user_id":"380","athlete_attendance_id":"48","coach_id":"302","athlete_id":"380","athlete_attendance":"1"}]} 

如果表1中沒有數據,則將空值替換為“ 0”,以顯示郵遞員的值

“athlete_attendance_id”: “空”, “coach_id”: “空”, “athlete_id”: “空”, “athlete_attendance”: “空”

{
    "responseCode": 200,
    "responseMessage": "Athlete details Successfully display",
    "data": [
        {
            "user_id": "377",
            "athlete_attendance_id": null,
            "coach_id": null,
            "athlete_id": null,
            "athlete_attendance": null
        }
]
}

我想要有價值的回應

{
        "responseCode": 200,
        "responseMessage": "Athlete details Successfully display",
        "data": [
            {
                "user_id": "377",
                "athlete_attendance_id": 0,
                "coach_id": 0,
                "athlete_id": 0,
                "athlete_attendance": 0
            }
    ]
    }

這是模特

            public function showAthleteData($team_id2,$coach_id2){
 $this->db->select('user.*,team.team_id,teams_athlete.team_id,dev_athlete_attendance.*');
          $table = array('user');
          $this->db->from($table);
                 $this->db->join('teams_athlete', 'user.user_id=teams_athlete.user_id');
                $this->db->join('dev_athlete_attendance' ,'dev_athlete_attendance.athlete_id = dev_teams_athlete.user_id','left' );
              $this->db->join('team','team.team_id = teams_athlete.team_id');
                $this->db->where('team.user_id',$coach_id2);

                $result = $this->db->get();
               if($result->num_rows() > 0 ){
                      return $result->result_array();
                                 }else{
                                     return 0;
                                }
 }

調節器

$team_id2= $this->input->post('team_id');
         $coach_id2= $this->input->post('coach_id'); //coach_id

         $userCount['result'] = $userCount1 = $this->Querydata->showAthleteData($team_id2,$coach_id2);
                    if($userCount['result']>0){


                         $data_arr1 = array(
                        "responseCode" =>  $this->res = 200,
                         "responseMessage" =>  $this->login = 'Athlete details Successfully display',
                        "data" =>$userCount['result']);
                       echo json_encode($data_arr1);

使用IFNULL檢查您的列是否為空,然后將默認值設置為0

例如:

   select IFNULL(coach_id, '0') AS coach_id,     
    IFNULL(athlete_id, '0') AS athlete_id,     
    IFNULL(athlete_attendance, '0') AS athlete_attendance ....

編輯:

SELECT dev_user.*, IFNULL(team.team_id, 0) as team_id1,
 IFNULL(teams_athlete.team_id, 0) as team_id2,
IFNULL(dev_athlete_attendance.coach_id,0) as coach_id,
IFNULL(dev_athlete_attendance.athlete_id,0) as athlete_id,
IFNULL(dev_athlete_attendance.athlete_attendance,0) as athlete_attendance 
FROM dev_user JOIN dev_teams_athlete ON dev_user.user_id=dev_teams_athlete.user_id 
LEFT JOIN dev_athlete_attendance ON dev_athlete_attendance.athlete_id = dev_teams_athlete.user_id 
JOIN dev_team ON dev_team.team_id = dev_teams_athlete.team_id 
WHERE dev_team.user_id = '301'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM