2010-01-15 60 views
3

我查询抛出这样的错误MySQL的选择查询错误

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 

的错误是

注意:未定义指数:cHospital1在F:\ WAMP \ WWW \ MMR-最终\ ADMIN \ all_hospital_list.php线97上

说明:未定义指数:cHospital2在F:\瓦帕\ WWW \ MMR-最终\管理员\ all_hospital_list.php上线98

不冰:未定义的索引:cHospital1在F:\ wamp \ www \ mmr-final \ admin \ all_hospital_list.php在线97

注意:未定义的索引:c:\ wamp \ www \ mmr-final \ admin \ all_hospital_list.php线98

说明:未定义指数:cHospital1在F:\瓦帕\ WWW \ MMR-最终\管理员\ all_hospital_list.php线97上

说明:未定义指数:cHospital2在F:\ wamp \ www \ mmr-final \ admin \ all_hospital_list.php on line 98

注意:未定义的索引:cHospital1在F:\ wamp \ www \ mmr-final \ admin \ all_hospital_list.php在线97

注意:未定义指数:cHospital2在F:\瓦帕\ WWW \ MMR-最终\管理员\ all_hospital_list.php上线98

说明:未定义指数:cHospital1在F:\瓦帕\ WWW \ mmr-线最终\管理员\ all_hospital_list.php 97

说明:未定义指数:cHospital2在F:\瓦帕\ WWW \ MMR-最终\管理员\ all_hospital_list.php上线98

说明:未定义指数:cHospital1在线97:F:\ wamp \ www \ mmr-final \ admin \ all_hospital_list.php

Noticias e:未定义的索引:在98行的F:\ wamp \ www \ mmr-final \ admin \ all_hospital_list.php中的cHospital2`

并且它也抛出Null行。

如何将此查询更改为不会导致错误?

+0

问题不在于查询,而在于PHP。你可以发布完整的文件或至少更多的周边代码? – Tom 2010-01-15 19:08:19

+0

http://pastebin.com/m4fbf313d – Bharanikumar 2010-01-15 19:23:20

+0

我告诉你我在想什么.. 看到我有三个字段,chospital,chospital1,chospital2,在tbl1表中,我有大约50个病人条目,有些病人可能有chostial1,chosptail2,有些病人有一个chopsital代码,我想渲染tbl2表中的所有代码,我不想在我的outout中重复,也没有null值.. – Bharanikumar 2010-01-15 19:26:07

回答

0

UNION查询中,所有行的列名必须相同。因此它使用联合中第一个查询的列名。联合中后续查询中的列名将被忽略。

换句话说,在此查询:

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 

列名的所有行是:idcHospital

在PHP代码

所以,当你获取结果作为对象,并试图引用对象的一个​​领域是这样的:

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

的对象不具有这些名称的字段。它只有$row->cHospital,即使是来自联合中第二个和第三个查询的行。

+0

的列数是相同的,我在这里需要改变 – Bharanikumar 2010-01-15 19:30:49

+0

只需在所有行中使用'$ row-> cHospital'。 – 2010-01-15 19:34:23

+0

现在查询是完美的,你看,我得到了,第一行为空,如何消除.. – Bharanikumar 2010-01-15 19:38:16

2

这些不是MySQL错误。他们是PHP错误。基本上你很可能试图在不首先声明它们的情况下使用变量。一定要在使用它们之前总是声明你的变量。它使你不喜欢有错误。

+0

嗨感谢指向..http://pastebin.com/ m4fbf313d 请看,,, – Bharanikumar 2010-01-15 19:20:31