0

I have a table named as Attendance, Here is the structure given below.

id     fname     lname    roll_no   date         time 
________________________________________________________
1      Qadir    Hussain   08cs18    19/04/2013   8:45am
2      Qadir    Hussain   08cs18    19/04/2013   8:50am
_______________________________________________________

i want to insert the record having roll_no = 08cs18 only one time per day. not more than once per day.

I can restrict this via if/else. but is it possible to restrict this via sqlite query?

Edit

Acctually i m making an app of studnets attendance, I m using the QR_code to scan encoded roll_no. once a student scan its card for the first time it should insert the record (i.e id = 1) if user again scan it on the same day it should not insert. means a particular student should have a attendance record only one time per day.

4

2 回答 2

2

对于这两个数据库,对roll_no和进行唯一键约束date

CREATE UNIQUE INDEX daily_roll_no ON Attendance (roll_no,date)

然后对于 mysql 使用该IGNORE子句进行插入。对于 sqlite,使用OR IGNORE.

 INSERT IGNORE INTO Attendance ....

 INSERT OR IGNORE INTO Attendance ....

mysql:

sqlite:

于 2013-04-19T21:35:01.887 回答
1

您可以在单个 SQL 中执行此操作:

INSERT OR REPLACE INTO <tablename> 
      (<identifier>, <time inserted>, column1, column2, ...) 
SELECT <identifier>, <time inserted>, 
       COALESCE(column1,'new value 1'), 
       COALESCE(column2,'new value 2'), ...
  FROM <tablename>
 WHERE <identifier> = ...
   AND <time inserted> > <now minus one day>

基本思想是:使用INSERT OR REPLACE您可以根据一个或多个条件更新现有行或插入新行。在这种情况下,如果该行不超过一天,我将使用相同的值更新该行。

如果该行不存在或超过 1 天,您可以使用 分配新值COALESCE

我知道这只是一种大纲,但可能会有所帮助....干杯!

于 2013-04-19T21:45:26.950 回答