简体   繁体   English

PHP中的MySQL UNION运算符错误

[英]MySQL UNION operator error in PHP

I have three mysql tables. 我有三个mysql表。

1. inbox - ID(int), FileNo(varchar), FolioNo(smallint), Reference(varchar), Received(date), Sent(date), Description(text), Forwarded(varchar),...etc

2. outbox - ID(int), Reference(varchar), Sent(date), FileNo(varchar), FileName(varchar), FolioNo(smallint), Description(text), Receiver(varchar),...etc

3. status - ID(int), SN(int), Date(date), Minutes(varchar), Instructed(varchar), Responsible(varchar), Action(boolean), FileNo(varchar),...etc

I want to join these three tables using FileNo and show them order by FolioNo as follows. 我想使用FileNo联接这三个表,并按FolioNo显示它们的顺序,如下所示。

File No: 123
File Name: abc
Folio No | Date     | Description | Sender/Receiver | Minutes   | Instructed | Responsible | Action
----------------------------------------------------------------------------------------------------
1        | 13-09-08 | Something   | Someone         | Something | Someone    | Someone     | Yes
2        | 13-09-10 | Something   | Someone         | Something | Someone    | Someone     | Yes
......etc

My SQL query in php is as follows, 我在php中的SQL查询如下

$sql="SELECT * FROM 
(SELECT I.FileNo, I.FolioNo, I.Received AS Date, I.Description, I.Forwarded 
FROM Inbox I WHERE I.FileNo='$ID' 

UNION SELECT O.FileName, O.FolioNo, O.Sent AS Date, O.Description, O.Receiver 
FROM Outbox O WHERE O.FileNo='$ID' 

UNION SELECT S.Date, S.Minutes, S.Instructed, S.Responsible, S.Action 
FROM Status S WHERE S.FileNo='$ID') 

AS A ORDER BY A.FolioNo";

Output, 输出,

<table>
<tr><th>Folio No</th><th>Date</th><th>Description</th><th>Sender/ Receiver</th><th>Minutes</th><th>Instructed</th><th>Responsible</th><th>Action</th></tr>
<?php                           
while ($list = mysql_fetch_assoc($result)) {
echo "<tr><td>" . $list['FolioNo'] . "</td><td>" . $list['Date'] . "</td><td>" . $list['Description'] . "</td><td>" . $list['Receiver'] . "</td><td>" . $list['Minutes'] . "</td><td>" . $list['Instructed'] . "</td><td>" . $list['Responsible'] . "</td><td>" . ($list['Action']=1?'Yes':'No') . "</td></tr>";
$x++;
}
echo "</table>";
?>

But this shows an error, 但这显示了一个错误,

Notice: Undefined index: Receiver in C:\\xampp\\htdocs\\sp\\viewMinutes.php on line 145 Notice: Undefined index: Minutes in C:\\xampp\\htdocs\\sp\\viewMinutes.php on line 145 Notice: Undefined index: Instructed in C:\\xampp\\htdocs\\sp\\viewMinutes.php on line 145 Notice: Undefined index: Responsible in C:\\xampp\\htdocs\\sp\\viewMinutes.php on line 145 Notice: Undefined index: Receiver in C:\\xampp\\htdocs\\sp\\viewMinutes.php on line 145 Notice: Undefined index: Minutes in C:\\xampp\\htdocs\\sp\\viewMinutes.php on line 145 Notice: Undefined index: Instructed in C:\\xampp\\htdocs\\sp\\viewMinutes.php on line 145 Notice: Undefined index: Responsible in C:\\xampp\\htdocs\\sp\\viewMinutes.php on line 145 Notice: Undefined index: Receiver in C:\\xampp\\htdocs\\sp\\viewMinutes.php on line 145 Notice: Undefined index: Minutes in C:\\xampp\\htdocs\\sp\\viewMinutes.php on line 145 Notice: Undefined index: Instructed in C:\\xampp\\htdocs\\sp\\viewMinutes.php on line 145 Notice: Undefined index: Responsible in C:\\xampp\\htdocs\\sp\\viewMinu 注意:未定义索引:第145行的C:\\ xampp \\ htdocs \\ sp \\ viewMinutes.php中的Receiver注意:未定义索引:第145行的C:\\ xampp \\ htdocs \\ sp \\ viewMinutes.php中的分钟注意:未定义索引:指示在第145行的C:\\ xampp \\ htdocs \\ sp \\ viewMinutes.php中注意:未定义的索引:在第145行的C:\\ xampp \\ htdocs \\ sp \\ viewMinutes.php中负责:注意:未定义的索引:在C:\\ xampp \\中的接收方第145行上的htdocs \\ sp \\ viewMinutes.php注意:未定义的索引:第145行上的C:\\ xampp \\ htdocs \\ sp \\ viewMinutes.php中的分钟注意:未定义的索引:在C:\\ xampp \\ htdocs \\ sp \\ viewMinutes中指示。第145行上的php注意:未定义的索引:第145行上的C:\\ xampp \\ htdocs \\ sp \\ viewMinutes.php中的负责人注意:未定义的索引:第145行的C:\\ xampp \\ htdocs \\ sp \\ viewMinutes.php中的接收者未定义的索引:第145行的C:\\ xampp \\ htdocs \\ sp \\ viewMinutes.php中的分钟注意:未定义的索引:145行的C:\\ xampp \\ htdocs \\ sp \\ viewMinutes.php中的说明:未定义的索引:在C中负责:\\ xampp \\ htdocs \\ sp \\ viewMinu tes.php on line 145 145行上的tes.php

FOLIO NO |  DATE    | DESCRIPTION   | SENDER/ RECEIVER  | MINUTES   | INSTRUCTED    | RESPONSIBLE   | ACTION
-------------------------------------------------------------------------------------------------------------------------------------
1        | 13-09-08 |   Something   |                   |           |               |               | Yes
2        | 13-09-08 |   Something   |                   |           |               |               | Yes
         | 13-09-08 |   Something   |                   |           |               |               | Yes
Something|  Someone |   Anyone  |               Yes

Whats the wrong here? 怎么了

UPDATE: 更新:

$sql = "SELECT 
Inbox.FolioNo, Inbox.Received, Inbox.Description, Inbox.Forwarded, 
Outbox.FileNo, Outbox.FileName, Outbox.FolioNo, Outbox.Sent, Outbox.Description, Outbox.Receiver, 
Status.Date, Status.Minutes, Status.Instructed, Status.Responsible, Status.Action 
FROM Status INNER JOIN Outbox ON Status.FileNo = Outbox.FileNo 
INNER JOIN Inbox ON Status.FileNo = Inbox.FileNo WHERE Status.FileNo = '$ID' ORDER BY Outbox.FolioNo ASC";

Your SELECT clause has no aliases mentioned. 您的SELECT子句未提及别名。 As a result, SQL engine does not know what column names to generate. 结果,SQL引擎不知道要生成什么列名。 I am assuming it takes the column names of the 1st SELECT. 我假设它采用第一个SELECT的列名称。 Hence you see undefined index for "Receiver" which is a part of 2nd SELECT. 因此,您会看到“接收器”的未定义索引,它是第二选择的一部分。

Also, I do not understand the logic behind the UNION. 另外,我不了解UNION背后的逻辑。 You are trying to UNION results of 3 different tables that have completely different columns. 您正在尝试对3个具有完全不同的列的不同表的结果进行UNION。

I had to make small change in output view. 我必须在输出视图中进行一些小的更改。 Now everything working fine. 现在一切正常。 My sql query is as follows. 我的SQL查询如下。

$sql="SELECT I.FileNo, I.FolioNo, I.Received AS Date, I.Sender AS Person, I.Description, I.Forwarded,  
S.Minutes, S.Instructed, S.Responsible, S.Action FROM Inbox I 
LEFT JOIN Status S ON I.ID=S.SN 
WHERE I.FileNo='$ID' 
UNION SELECT O.FileName, O.FolioNo, O.Sent AS Date, O.Description, O.Receiver AS Person, O.Reference, O.FileNo, O.Signed, O.Subject, O.ID 
FROM Outbox O WHERE O.FileNo='$ID'";

Thanks guys! 多谢你们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM