0

我有两个表(employee& ),我想在没有为用户找到记录的时候log_sheet插入今天的日期。如果有记录,它应该返回一些值。log_sheetlog_sheet

SELECT employee.id,
       employee.name,
       log_sheet.`date`
FROM   employee
       LEFT OUTER JOIN log_sheet
                    ON employee.id = log_sheet.employee_id
WHERE  employee.name = 'admin' 
4

2 回答 2

0

所以你想确保每个员工至少有一个 log_sheet,对吧?

像这样的东西应该工作:

INSERT INTO log_sheet (employee_id, date)
    SELECT id, DATE(NOW()) 
    FROM   employee
    WHERE  name = 'admin' 
      AND  id NOT IN ( SELECT DISTINCT employee_id FROM log_sheet ) 

要预览这会做什么,请#在该INSERT行之前添加。

于 2012-11-14T23:11:49.600 回答
0

试试下面的查询:

insert into log_sheet (date, employee_id) 
(
SELECT now(),
    employee.id
FROM employee
LEFT OUTER JOIN log_sheet ON employee.id = log_sheet.employee_id
WHERE employee.name = 'admin' AND log_sheet.`date` IS NULL
)
于 2012-11-14T23:14:47.313 回答