我想在向表1中插入新记录时,根据其他表值为DATETIME分配一个值,但是无法将它们放在一起。
TABLE1有interval_id和submit_date列(以及许多其他列)
TABLE2有interval_id,begin_time和end_time
示例:
到目前为止:
INSERT INTO `TABLE1` (null, now())
WHERE
DATE(submit_date) = curdate()
AND TIME(submit_date)
----不确定编码在这里将TIME与开始和结束时间列数据进行比较----
完整插入声明:
$sql = "INSERT INTO `summary` VALUES (null,'$user_id','$task_id',NOW(),'$tcompleted_id','$minutes_id','$hourinterval_id')
SELECT NOW(), hourinterval_id
FROM `hour_interval` H
WHERE TIME(NOW()) BETWEEN H.start_hour AND H.end_hour LIMIT 1"
答案 0 :(得分:1)
您可以插入SELECT
声明的结果:
INSERT INTO `TABLE1`(curdate, interval_id)
SELECT now(), inteval_id FROM `TABLE2` t2
WHERE TIME(now()) BETWEEN t2.start_time AND t2.end_time LIMIT 1