如何獲取某個作業的最後一步並確定每個作業的最後一個進程的狀態是否等於1.如果最後一個進程的狀態等於然後顯示值。獲取每個作業的最後進程並識別狀態
作業表(prepressjobs)
+-----------+--------------+------------+
| ID | SchedNum | Item Name |
+-----------+--------------+------------+
| 1 | JITE7ERUK | Item 1 |
| 2 | JV7FSW26Y | Item 2 |
| 3 | JQFXV4H3X | Item 3 |
+-----------+--------------+------------+
動作表(操作)
+-----------+--------------+------------+-----------+----------+
| ID | SchedNum | Job Name | Step | Status |
+-----------+--------------+------------+-----------+----------+
| 1 | JITE7ERUK | Designing | 0 | 1 |
| 2 | JITE7ERUK | Sample | 1 | 1 |
| 3 | JITE7ERUK | Printing | 2 | 0 |
| 4 | JV7FSW26Y | Designing | 0 | 1 |
| 5 | JV7FSW26Y | Sample | 1 | 0 |
| 6 | JQFXV4H3X | Designing | 0 | 1 |
| 7 | JQFXV4H3X | Sample | 1 | 1 |
+-----------+--------------+------------+-----------+----------+
MY QUERY
SELECT *
FROM operation p1
INNER JOIN prepressjobs
ON prepressjobs.pj_schednum = p1.operation_schednum
WHERE EXISTS(SELECT MAX(operation_step) FROM (SELECT DISTINCT(operation_schednum) FROM operation)p2)
AND p1.operation_status = 0 ;
預期的結果
+-----------+--------------+------------+-----------+----------+
| 7 | JQFXV4H3X | Sample | 1 | 1 |
+-----------+--------------+------------+-----------+----------+
它是MySQL或MS SQL? – Aparna
@Aparna,mySQL。 – Ailyn
@Aparna。它最有可能得到像使用此代碼的最後一行SELECT m1。* FROM操作m1 LEFT JOIN操作m2 ON(m1.operation_schednum = m2.operation_schednum AND m1.operation_id
Ailyn