使用mysql语句编程校园考勤系统
时间: 2024-12-15 10:22:58 浏览: 4
在MySQL中编程校园考勤系统,通常会涉及到创建数据库表、插入学生信息、记录签到状态等操作。以下是一些基本的SQL语句示例:
1. **创建表格(Tables)**:
- 学生表 (`students`):
```sql
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
class INT
);
```
- 签到记录表 (`attendance`):
```sql
CREATE TABLE attendance (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT,
date DATE,
check_in BOOLEAN,
FOREIGN KEY (student_id) REFERENCES students(id)
);
```
2. **插入数据(Insert Data)**:
```sql
INSERT INTO students (id, name, class) VALUES (1, '张三', 1);
INSERT INTO students (id, name, class) VALUES (2, '李四', 2);
```
3. **查询签到情况(Query Attendance)**:
- 查询特定学生的签到记录:
```sql
SELECT * FROM attendance WHERE student_id = 1;
```
- 统计班级的平均出勤率:
```sql
SELECT class, AVG(CASE WHEN check_in THEN 1 ELSE 0 END) as average_attendance
FROM attendance
GROUP BY class;
```
4. **更新考勤记录(Update Attendance)**:
```sql
UPDATE attendance SET check_in = true WHERE id = 1 AND date = '2023-03-01';
```
5. **删除数据(Delete Data)**:
删除过期或者不需要的签到记录:
```sql
DELETE FROM attendance WHERE date < CURDATE() - INTERVAL 1 MONTH;
```
阅读全文