我有两个表,第一个表有一些数据,第二个表第二个,所有数据现在都保留在联接查询中,并显示在json响应中null值如何替换空值我正在使用邮递员,在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"}]}
如果表1st中没有数据 然后将空值替换为“ 0”,以显示邮递员的响应
“ 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": 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);
答案 0 :(得分:1)
使用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'