简体   繁体   中英

DATE_ADD doesnt work

I have three tables in MySQL. Staff with fields id, name, surname, telephone, adress, id_work.

Work with fields id, name.

Absence with fields id, name, id_staff start_date and duration.

I have the following query:

   SELECT COUNT(*) 
     FROM staff s 
     JOIN work w
       ON s.id_work = w.id 
LEFT JOIN absence a 
       ON s.id = a.id_staff AND a.name = 'vacations' 
    WHERE (w.name='sales manager' AND a.id_staff IS NULL) 
       OR (w.name='sales manager') 
      AND (a.start_date>NOW () OR DATE_ADD (a.start_date,INTERVAL a.duration DAY)< NOW ())

I want to study the case of existing employees that have not absense and the case of existing employees that having absense but this is not priogramming for today. The main concept is this query returns the staff of a business who is present. My problem is that a person who has totaly two abseses eg one was for 11/7/2017 and the duration was for 10 days and another is for 19/8/2017 and the duration is for 5 days , the query returns count ( )=1 which is wrong. I want to display count ( )=0 because today this employee is present in his job

OR DATE_ADD(a.start_date, INTERVAL a.duration DAY) < NOW ()

your condition OR (w.name='sales manager') adds ALL the cases where w.name = sales Manager you Need to put brackets around the first two conditions (i think)

EDIT: i mean like this

    WHERE ((w.name='sales manager' AND a.id_staff IS NULL) 
       OR w.name='sales manager') 

but it doesn't make sense, since the first condition is negated by the second, and i don't really understand what you want to achieve

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM