MySQL select query Error

My request throws an error like

select id,cHospital from med_patient where cHospital is not null  union
select id,cHospital1 from med_patient where cHospital1 is not null  union
select id,cHospital2 from med_patient where cHospital2 is not null  order by 1

Error

Note: Undefined index: cHospital1 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 97

Note: Undefined index: cHospital2 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 98

Note: Undefined index: cHospital1 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 97

Note: Undefined index: cHospital2 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 98

Note: Undefined index: cHospital1 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 97

Note: Undefined index: cHospital2 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 98

Note: Undefined index: cHospital1 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 97

Note: Undefined index: cHospital2 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 98

Note: Undefined index: cHospital1 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 97

Note: Undefined index: cHospital2 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 98

Note: Undefined index: cHospital1 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 97

Note: Undefined index: cHospital2 in F: \ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 98`

And also it throws a zero line.

How can I modify this request so as not to cause an error?

+3
source share
2 answers

In the query, the UNIONcolumn names must be the same for all rows. Therefore, it uses the column names from the first query in the join. Column names in subsequent queries in the join are ignored.

, :

select id,cHospital from med_patient where cHospital is not null union 
select id,cHospital1 from med_patient where cHospital1 is not null union 
select id,cHospital2 from med_patient where cHospital2 is not null order by 1

: id cHospital.

, PHP-, :

$cHospital1 = $row->cHospital1;
$cHospital2 = $row->cHospital2;

. $row->cHospital, , .

0

MySQL. PHP. , , , . . .

+2

Source: https://habr.com/ru/post/1728671/


All Articles