$sql = "SELECT Status FROM citydoc2023.status WHERE EmailNotifierFlag = 1";
$record = $database->query($sql);
$statuses = '';
while ($data = $database->fetch_array($record)) {
$status1 = $data['Status'];
$statuses .= ",'".$status1."'";
}
$query = "SELECT a.TrackingNumber, a.Office,
IF(a.trackingType = 'PR', 'Purchase Request', IF(a.trackingType = 'PO', 'Purchase Order', a.DocumentType)) AS DocumentType,
a.Status AS DocumentStatus,
a.DateModified,
a.DelayedDays,
b.Email, b.OfficeCode, b.Office AS SenderOffice,
e.Name AS OfficeName, a.Year,
IF(a.TrackingType = 'PO', IF(a.totalAmountMultiple > 0, a.totalAmountMultiple, a.PO_Amount), IF(a.totalAmountMultiple > 0, a.totalAmountMultiple, a.Amount) ) AS Amount,
b.EmailNotifierFlag
FROM (
SELECT TrackingNumber, Status, Office, DocumentType, DateModified, Year, TrackingType, Amount, PO_Amount, totalAmountMultiple, DATEDIFF(SUBSTR(CURDATE(), 1, 10), SUBSTR(DateModified, 1, 10)) AS DelayedDays
FROM citydoc2023.vouchercurrent
WHERE Status IN (".substr($statuses, 1).")
GROUP BY TrackingNumber
UNION ALL
) a
LEFT JOIN citydoc2023.status b ON a.Status = b.Status
LEFT JOIN office e ON a.Office = e.Code
WHERE a.DelayedDays >= 3
GROUP BY a.TrackingNumber ORDER BY Email, a.DelayedDays DESC";
Here is the first example of the query where it will be able to fetch data from that data it will be able to fetch tracking numbers according to their respective offices. Offices that are shown in the database using the query are 5 different offices namely City Engineers, City Budget, City Administrator, General Service, and City Accountant. Now here is the second query
tSELECT
a.TrackingNumber,
a.Office,
IF(a.trackingType = 'PR', 'Purchase Request', IF(a.trackingType = 'PO', 'Purchase Order', a.DocumentType)) AS DocumentType,
a.Status AS DocumentStatus,
a.DateModified,
a.DelayedDays,
b.Email,
b.OfficeCode,
b.Office AS SenderOffice,
e2023.Name AS OfficeName,
a.Year,
IF(a.TrackingType = 'PO', IF(a.totalAmountMultiple > 0, a.totalAmountMultiple, a.PO_Amount), IF(a.totalAmountMultiple > 0, a.totalAmountMultiple, a.Amount) ) AS Amount,
b.EmailNotifierFlag
FROM (
SELECT *, '2023' AS DynamicYear FROM (
SELECT
TrackingNumber,
Status,
Office,
DocumentType,
DateModified,
Year,
TrackingType,
Amount,
PO_Amount,
totalAmountMultiple,
DATEDIFF(SUBSTR(CURDATE(), 1, 10), SUBSTR(DateModified, 1, 10)) AS DelayedDays
FROM citydoc2023.vouchercurrent
WHERE Status IN ('For Inspection','Pending at CBO','CAO Received ','Pending at CAO ','Pending at Admin ','Pending at CBO','GSO Received','Pending at GSO','Admin Received','Pending at Admin','GSO Received','Pending at GSO','Admin Received','Pending at Admin','Serve to Supplier','Supplier Conformed','Fund Control','Pending at CBO','For Inspection','Inventory','Pending at GSO - Inventory','CAO Received','Pending at CAO','On Evaluation - Accounting','Evaluated - Accounting','Forwarded to Admin - Administration')
GROUP BY TrackingNumber
) AS subquery2023
UNION ALL
SELECT *, '2024' AS DynamicYear FROM (
SELECT
TrackingNumber,
Status,
Office,
DocumentType,
DateModified,
Year,
TrackingType,
Amount,
PO_Amount,
totalAmountMultiple,
DATEDIFF(SUBSTR(CURDATE(), 1, 10), SUBSTR(DateModified, 1, 10)) AS DelayedDays
FROM citydoc2024.vouchercurrent
WHERE Status IN ('For Inspection','Pending at CBO','CAO Received ','Pending at CAO ','Pending at Admin ','Pending at CBO','GSO Received','Pending at GSO','Admin Received','Pending at Admin','GSO Received','Pending at GSO','Admin Received','Pending at Admin','Serve to Supplier','Supplier Conformed','Fund Control','Pending at CBO','For Inspection','Inventory','Pending at GSO - Inventory','CAO Received','Pending at CAO','On Evaluation - Accounting','Evaluated - Accounting','Forwarded to Admin - Administration')
GROUP BY TrackingNumber
) AS subquery2024
) a
LEFT JOIN citydoc2023.status b ON a.Status = b.Status
LEFT JOIN office e2023 ON a.Office = e2023.Code
LEFT JOIN citydoc2024.status c ON a.Status = c.Status
LEFT JOIN office e2024 ON a.Office = e2024.Code
WHERE a.DelayedDays >= 3
GROUP BY a.TrackingNumber
ORDER BY Email, a.DelayedDays DESC
ype here
it will only be able to show 4 offices unlike the first example which can show 5 offices and I saw that the data from the missing office can be found merged on one of the offices shown in the database. Pardon for the grammar. I just want to have similar results in getting data. P.S. Not a native English speaker because I am from a poor country.
I just want to have similar results in getting data. from the second example of query to the first example of query.