2010-01-15 56 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