如何检查数据库中是否已存在特定日期

时间:2012-02-07 12:12:47

标签: php mysql

有一个表,其中包含员工登录和注销的日期时间。我必须根据此日期时间设置其状态登录或注销。我必须在日期时间应用条件,如果数据库第一次存在日期,那么它将是登录,同一日期存在于同一员工的第二次,然后它将被注销。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

如果我说得对,下一个代码可以帮到你。 (这是怎么做的主要想法) 我假设桌子是这样的:

+ workers_logs

  • ID
  • worker_id
  • actiontime
  • BLA

我们将有一个名为workers的数组,其结构将是:

workers[ worker_id ] [0] = 'login'
workers[ worker_id ] [1] = 'logout'
workers[ worker_id ] [2] = 'login'
workers[ worker_id2 ] [0] = 'login'
workers[ worker_id2 ] [1] = 'logout'
workers[ worker_id2 ] [2] = 'login'

//代码:

$workers = array();
$getWorkersLogs = mysql_query("SELECT worker_id,actiontime FROM workers_logs ORDER BY id ASC"); //Notice for: ORDER BY id ASC - it's very imporatnt in your case.
while($logs = mysql_fetch_array($getWorkersLogs))
{
 if((!array_key_exists($logs['worker_id'] , $workers)) || count($workers[$log['worker_id']]) % 2 == 0 )
 {
   //This is the first time we are dealing with this worker (so it's login)
   //or it's an even number.
       .....
       .....

   //Add this worker id to our array so next time will be logout
     $workers[$logs['worker_id']][] = 'login'
 }
 else
 {
     $workers[$logs['worker_id']][] = 'logout'

 }
}