1
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';

This query returns the following, using PHP and the MySQL workbench:

Serial Number Assistant ID Reviewer ID Forename Surname Status Validation


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

However, when I run the following query:

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';

Returns only:

5 rows in PHP

7 rows in MySQL workbench

It does not return FREDA and ANTHONY's records and it spools a lot of spaces to the end of the query.

I have tried both a FOR LOOP and a 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
    }
}

I had to anonymize a lot of this so the queries might not match up. However, they are both identical except for the "tblreview.ReviewerID = tblreviewer.ReviewerID" join and code that goes with that.

Edit:

It gets worse. That was only a basic test query.

There is a validation name and a manager name. Both need to be returned to a table. Both records of the review and the validation are in the same table.

There was already a join that goes like this:

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

Adding the other two joins from the answer makes the query return nothing.

Have to run the sub-select join first then the two other joins to the other tables. Then in the two joins to other tables you have to explicitly reference the table you want. So in my case the main select had "tblname1 v " and then sub-select was " r ". Then you reference v.ID = othertbl.ID

1
  • Use LEFT JOIN instead of WHERE. Commented Nov 3, 2015 at 13:50

1 Answer 1

2

Please try to make the relations with 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Works perfectly. Can you explain why it needs this?
Please read this article for understanding "what is the difference between where and join relation": tutorialspoint.com/mysql/mysql-using-joins.htm
Thanks. Can you also explain how the third join can integrate with everything else?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.