duanchuang6978 2014-06-26 06:12
浏览 35
已采纳

MySQL选择“previous”max(id)WHERE

I have a table that inserts data when a kid checks in into a summer camp area. If the kid is < than 12 years of age a parents barcode must be scanned to allow the kid entry into the young kids area (<12 years of age).

The table has the following columns.

kcID
uID
id
age
room
barcode
date
amber

The data will usually be presented like this.

kcID uID    id      age     room    barcode date                    amber
25   1      1       30      1000    0001    6/26/2014 1:27:40 AM    0
26   6      1       1       1000    0005    6/26/2014 1:27:40 AM    0

The problem I have is that I need to compare the dates/hours to know if the kid is entering or leaving the camp area and via php send an SMS to the parents so they know their kid is outside a particular area.

I know I can retrieve the max(kcID) WHERE barcode = XXXX and that will return the last inserted row, but, in order for me to retrieve said information the kid must be scanned, properly inserting a new row and rendering max(kcID) useless in this case.

What I need is to be able to select max(kcID) WHERE barcode = xxxx and then select the previous row record in which barcode = xxxx is found. That way I can compare dates and know if the kid is leaving or entering that particular area.

The easiest solution I can think of right now is to have 2 tables (1 for entry 1 for out) and have the camp counselors choose if the kid is entering or leaving but I'm wondering if I can use only 1 table.

  • 写回答

2条回答 默认 最新

  • dongyan3018 2014-06-26 06:22
    关注

    You could add a column indicating leave / return, and add this to your condition.

    e.g.

    ALTER TABLE `table`
        ADD `status` enum('left', 'returned') NOT NULL DEFAULT 'left';
    

    a query would be

    SELECT `kcID`, `barcode`
    FROM `table`
    WHERE `status` = 'left'
    ORDER BY `timefield` DESC
    LIMIT 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面