从更好的解决方案角度来看,最好在应用程序逻辑中将数据插入到使用表中时执行此操作,而不是创建触发器来实现此目的。为了演示你的风格,我使用了一个存储过程来插入使用表。您可以相应地修改它。
-- create usage table
create table `usage` (job varchar(100),
start_time date,
end_time date,
`session` varchar(100)
);
-- create `sessions` table
create table `sessions` (session_label varchar(100) ,
start_time date,
end_time date);
-- create procedure prc_insert_into_usage
delimiter $$
Create Procedure prc_insert_into_usage
(
IN p_job varchar(100),
IN p_start_time date,
IN p_end_time date
)
Begin
Declare v_session varchar(100);
-- check if record is there in the session table
-- it is assumed session table will have only one record for composite key (start_time, end_time)
Select session_label into v_session
from `sessions`
where start_time = p_start_time and end_time = p_end_time;
IF (v_session IS NULL) THEN
-- if session_label is generated using auto increment key in sessions table then
-- last_insert_id() can be used to fetch that value after insert into sessions table
-- which can be used while inserting into usage table
-- example below
-- Insert into `sessions` (start_time,end_time) values (p_start_time,p_end_time);
-- set @v_session = last_insert_id();
-- Insert into `usage` values (p_job,p_start_time,p_end_time,@v_session);
-- dummy logic to create the session_label
-- dummy logic is used you can replace it with whatever logic you need
set @v_session = concat('sess_',left(uuid(),8));
-- insert record in both table (first in session table and then in usage table)
Insert into `sessions` values (@v_session,p_start_time,p_end_time);
Insert into `usage` values (p_job,p_start_time,p_end_time,@v_session);
else
-- record already present in sessions table for this session label
-- so insert record only in usage table
Insert into `usage` values (p_job,p_start_time,p_end_time,@v_session);
End If;
End;
$$
-- call this procedure to insert data into usage table
-- sample execution below when both tables have no rows
-- below call will insert 1 rows in both table having same session label
mysql> call prc_insert_into_usage('job_a','2015-01-01','2015-01-02');
Query OK, 1 row affected (0.00 sec)
mysql> select * from `usage`;
+-------+------------+------------+---------------+
| job | start_time | end_time | session |
+-------+------------+------------+---------------+
| job_a | 2015-01-01 | 2015-01-02 | sess_abc376bf |
+-------+------------+------------+---------------+
1 row in set (0.00 sec)
mysql> select * from `sessions`;
+---------------+------------+------------+
| session_label | start_time | end_time |
+---------------+------------+------------+
| sess_abc376bf | 2015-01-01 | 2015-01-02 |
+---------------+------------+------------+
1 row in set (0.01 sec)
-- below call will insert only in usage table as row already present in sessions table
mysql> call prc_insert_into_usage('job_b','2015-01-01','2015-01-02');
Query OK, 1 row affected (0.00 sec)
mysql> select * from `usage`;
+-------+------------+------------+---------------+
| job | start_time | end_time | session |
+-------+------------+------------+---------------+
| job_a | 2015-01-01 | 2015-01-02 | sess_abc376bf |
| job_b | 2015-01-01 | 2015-01-02 | sess_abc376bf |
+-------+------------+------------+---------------+
2 rows in set (0.00 sec)
mysql> select * from `sessions`;
+---------------+------------+------------+
| session_label | start_time | end_time |
+---------------+------------+------------+
| sess_abc376bf | 2015-01-01 | 2015-01-02 |
+---------------+------------+------------+
1 row in set (0.00 sec)
-- below call will again insert rows in both table
mysql> call prc_insert_into_usage('job_c','2015-01-02','2015-01-04');
Query OK, 1 row affected (0.01 sec)
mysql> select * from `usage`;
+-------+------------+------------+---------------+
| job | start_time | end_time | session |
+-------+------------+------------+---------------+
| job_a | 2015-01-01 | 2015-01-02 | sess_abc376bf |
| job_b | 2015-01-01 | 2015-01-02 | sess_abc376bf |
| job_c | 2015-01-02 | 2015-01-04 | sess_dcfa6853 |
+-------+------------+------------+---------------+
3 rows in set (0.00 sec)
mysql> select * from `sessions`;
+---------------+------------+------------+
| session_label | start_time | end_time |
+---------------+------------+------------+
| sess_abc376bf | 2015-01-01 | 2015-01-02 |
| sess_dcfa6853 | 2015-01-02 | 2015-01-04 |
+---------------+------------+------------+
2 rows in set (0.00 sec)
mysql>
不要使用触发器来做到这一点。当你读取数据时使用'JOIN'来做 - 除非你有一个真正的实时理由。 –
我已经更新了这个问题,你可以帮我实现这个使用触发器,或者如果联接更好,如何以及为什么? –