简体   繁体   中英

Get count and show order number to display

I have a table which pulls this:

 $nurse_query = "SELECT * FROM patientinfo WHERE SCHDT = '$currentdate'
 AND SURGEON NOT LIKE 'NOT ON FILE' OR SCHDT = '$currentdate' AND TASK
 LIKE 'CLINIC%'  Order By COLBY_Arrival_time_1,SCHTM ASC";

It displays all the surgeries we have for a day ordered by arrival time and surgery time. Works well. Now they want me to get the Order Number of the patient based on the surgeon and surgery time. So for example:

If the first six surgeries are:

Patient       Surgery Time        Surgeon
Smith          0800                Lees
Johnson        0815                Kaiser
Minnie         0800                Pappy
Niehaus        0900                Pappy
Kurle          0930                Lees
Kusiek         1000                Kaiser
Johnson        1000                Pappy

I would display it like this

Surgery Time       Patient        Surgeon      Order
0800               Smith           Lees          1
0800               Minnie          Pappy         1
0815               Johnson         Kaiser        1
0900               Niehaus         Pappy         2
0930               Kurle           Lees          2
1000               Kusiek          Kaiser        2
1000               Johnson         Pappy         3

When a new patient is added, it should automatically reorder the order number.

Try this:

select surgery_time, patient, surgeon, count(surgeon) as ord from patientinfo group by surgeon,surgery_time order by surgery_time,ord

+--------------+---------+---------+-----+
| surgery_time | patient | surgeon | ord |
+--------------+---------+---------+-----+
| 0800         | smith   | lees    |   1 |
| 0800         | Minnie  | Pappy   |   1 |
| 0815         | johnson | kaiser  |   1 |
| 0900         | Niehaus | Pappy   |   1 |
| 0930         | Kurle   | lees    |   1 |
| 1000         | kusiek  | kaiser  |   1 |
| 1000         | Johnson | Pappy   |   1 |
+--------------+---------+---------+-----+

Only thing it doesn't show each time count

maybe this is table data you want to show

Patient Surgery     Time Surgeon
Smith Lees          0800 
Johnson Kaiser      0815 
Minnie Pappy        0800 
Niehaus Pappy       0900 
Kurle Lees          0930 
Kusiek Kaiser       1000 
Johnson Pappy       1000 


Surgery Time    Patient         Surgeon Order 
0800            Smith Lees      1 
0800            Minnie  Pappy   1 
0815            Johnson Kaiser  1 
0900            Niehaus Pappy   2 
0930            Kurle Lees      2
1000            Kusiek Kaiser   2 
1000            Johnson Pappy   3

to use like this, try here

It is impossible to know table names/column names but I am assuming you need to get info from two different tables in which case this may be a good guidline.

Joins, in this case INNER JOIN, allow you to also pull a row from a different table based on similar fields to the current row in the current table.

SELECT 
    *
FROM 
    patientinfo
LEFT JOIN otherTable ON patientinfo.SCHDT  = otherTable.SCHDT AND patientinfo.SURGEON = otherTable.SURGEON
WHERE
    (patientinfo.SCHDT = '$currentdate' AND patientinfo.SURGEON NOT LIKE 'NOT ON FILE')
    OR
    (patientinfo.SCHDT = '$currentdate' AND patientinfo.TASK LIKE 'CLINIC%')
ORDER BY 
    COLBY_Arrival_time_1, SCHTM ASC";

EDIT

Thanks for some clarification but you should really have still posted column names for us to work with.

Anyway, I was able to get what would wanted with another table of mine. It is a bit far fetched and someone may jump in with a better approach but this is what I got for now.(This will need analysis by you to make work for your particular table)

SELECT 
    info.*,
    COUNT(j.userFirstName) + 1
FROM 
    patientinfo info
LEFT JOIN(
    SELECT * FROM patientinfo ORDER BY COLBY_Arrival_time_1, SCHTM ASC
) joininfo ON info.SURGEON = joininfo.SURGEON AND info.SCHDT = joininfo.SCHDT AND info.COLBY_Arrival_time_1 > joininfo.COLBY_Arrival_time_1
WHERE
    (info.SCHDT = '$currentdate' AND info.SURGEON NOT LIKE 'NOT ON FILE')
    OR
    (info.SCHDT = '$currentdate' AND info.TASK LIKE 'CLINIC%')
ORDER BY 
    COLBY_Arrival_time_1, SCHTM ASC
GROUP BY 
    info.SURGEON, 
    info.COLBY_Arrival_time_1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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