PHP MySQL缺少行(bug?)

时间:2015-11-03 13:45:55

标签: php mysql

SELECT 
  tblassistant.identNumber, 
  tblreview.ReviewID, 
  tblreview.Status, 
  tblreview.AssistantId AS AssistID, 
  tblassistant.Forename AS AssistForename, 
  tblassistant.Surname AS AssistSurname, 
  tblreview.Validation 
FROM tblreview, tblassistant 
WHERE 
  tblreview.Validation = 'Y' 
  AND tblassistant.Test <> 'Y'
  AND tblassistant.identNumber = tblreview.assistantidentNumber 
  AND YEAR(STR_TO_DATE(tblassistant.DateDied,'%d/%m/%Y')) = '2014'
  AND MONTH(STR_TO_DATE(tblassistant.DateDied,'%d/%m/%Y')) = '9';

此查询使用 PHP MySQL工作台返回以下内容:

序列号助理ID审阅者ID Forename姓氏状态验证

4144449170 55 46 JESSICA BLACK完成Y
4744443020 56 46 Maureen Gar Y
4744443620 57 46 SARAH RAN完成Y
4744440430 58 46 LOUISE WHIT Y
6144448966 59 48 FIONA LEW完成Y
4444443238 60 48 TONY BRETH Y
4744442765 61 46 OLIVIA DAVIE完成Y

但是,当我运行以下查询时:

SELECT 
  tblreview.AssistantIdentNumber, 
  tblreview.AssistID,
  tblreview.Status,
  tblreview.ReviewerId AS RevID,
  tblAssistant.Forename AS RevForename,
  tblAssistant.Surname AS RevSurname,
  tblreview.Validation 
FROM tblreview, tblreviewer, tblAssistant
WHERE
  tblreview.ReviewerID = tblreviewer.ReviewerID
  AND tblreview.Validation = 'Y'
  AND tblAssistant.Test <> 'Y'
  AND tblAssistant.IdentNumber = tblreview.AssistantIdentNumber
  AND YEAR(STR_TO_DATE(tblAssistant.DateDied,'%d/%m/%Y')) = '2014'
  AND MONTH(STR_TO_DATE(tblAssistant.DateDied,'%d/%m/%Y')) = '9';

仅返回:

PHP中的5行

MySQL工作台中的7行

它没有返回FREDA和ANTHONY的记录,并且在查询结束时将大量空格假脱机。

我尝试了FOR LOOP和WHILE LOOP

for($i=0;$i<$rowCount;$i++) 
{
    $row[$i]= mysql_fetch_array($ValidationPart);
    // writes out HTML table
}

for($i=0;$i<$rowCount;$i++) 
{
    $row[$i]= mysql_fetch_array($ValidationPart);
    if (empty($row[$i]['IdentNumber']))
    { 
        //do nothing
    } else {
        // Prints out HTML table
    }
}

我不得不对此进行匿名处理,以便查询可能不匹配。但是,它们都是相同的,除了&#34; tblreview.ReviewerID = tblreviewer.ReviewerID&#34;加入和代码。

修改

情况变得更糟。那只是一个基本的测试查询。

有验证名称和经理名称。两者都需要返回到表中。审核和验证的两个记录都在同一个表中。

已经有一个像这样的联接:

LEFT JOIN 
(SELECT tblreview.AssistantIdentNumber,
tblreview.AssistId,
tblreview.Status, 
tblreview.ManagerId AS RevID, 
tblreviewer.Forename AS RevForename, 
tblreviewer.Surname AS RevSurname 
FROM tblreview, tblreviewer 
WHERE tblreview.ReviewerId = tblreviewer.ReviewerId 
AND tblreview.Status = 'Complete' 
AND IsNull(tblreview.Validation)) r

从答案中添加其他两个联接会使查询不返回任何内容。

必须先运行子选择连接,然后再将其他两个连接连接到其他表。然后在与其他表的两个连接中,您必须显式引用所需的表。所以在我的情况下,主要的选择有&#34; tblname1 v&#34;然后再选择是&#34; r&#34;。然后你引用v.ID = othertbl.ID

1 个答案:

答案 0 :(得分:2)

请尝试与LEFT JOIN建立关系:

SELECT 
  tblreview.AssistantIdentNumber, 
  tblreview.AssistID, 
  tblreview.Status, 
  tblreview.ReviewerId AS RevID, 
  tblAssistant.Forename AS RevForename, 
  tblAssistant.Surname AS RevSurname, 
  tblreview.Validation 
FROM tblreview
LEFT JOIN tblreviewer
  ON tblreview.ReviewerID = tblreviewer.ReviewerID
LEFT JOIN tblAssistant 
  ON tblAssistant.IdentNumber = tblreview.AssistantIdentNumber 
WHERE 
  tblreview.Validation = 'Y'
  AND tblAssistant.Test <> 'Y' 
  AND YEAR(STR_TO_DATE(tblAssistant.DateDied,'%d/%m/%Y')) = '2014' 
  AND MONTH(STR_TO_DATE(tblAssistant.DateDied,'%d/%m/%Y')) = '9';

Read more about Joins.