获取计数并显示要显示的订单号

时间:2015-07-16 14:06:41

标签: php mysql

我有一张桌子可以说明这一点:

loadDataInBackground

它显示我们按照到达时间和手术时间排序的所有手术。效果很好。现在,他们希望我根据外科医生和手术时间获得患者的订单号。例如:

如果前六次手术是:

 $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";

我会像这样显示它

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

添加新患者时,应自动重新订购订单号。

3 个答案:

答案 0 :(得分:0)

试试这个:

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 |
+--------------+---------+---------+-----+

只有每次计数都没有显示

答案 1 :(得分:0)

也许这是您想要展示的表格数据

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

像这样使用,try here

答案 2 :(得分:0)

不可能知道表名/列名,但我假设您需要从两个不同的表中获取信息,在这种情况下,这可能是一个很好的指导。

连接,在本例中为INNER JOIN,允许您根据与当前表中当前行相似的字段从另一个表中提取一行。

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

修改

感谢您的一些澄清,但您确实应该发布我们要使用的列名。

无论如何,我能够用另一张桌子得到想要的东西。 这有点牵强,有人可能会采用更好的方法,但这就是我现在所得到的。(这需要你分析才能为你的特定桌子工作)

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