php - Select rows from specific date with left join -
I am currently working on a simple employee scheduling tool and there is a problem with SQL queries. To explain my problem, use these two very abstract tables.
In the first table, there are just employees
employee ====================== empId | | Name | ... ---------------------- 10 | Scott | 11 | Schrute | 12 | Halpert | 13. Howard |In the second table you find the work assigned to each employee by day.
Tasks ========================================== ==== tasId | Name | Work | Date | ... ---------------------------------------------- 10 | Scott | Support | 2014-02-17 | 11 | Scott | Bugfix | 2014-02-18 | 12 | Halpert | Bugfix | 2014-02-17 | 13. Halpert | Development | 2014-02-18 | 14 | Howard | Support | 2014-02-17 |Now I want to know what the employees are doing on 17th February or if there is no work plan for that day. I use the following SQL query to do this.
Select E. Names, employees to vaccinate and E.name = TNN Add functionality where the date is zero or date = date ('2014-02-17')
The result is what I want:
name. Work -------------------- Scott | Help Shrauth. NULL Halpert | Bugfix Howard | HelpAnd now my problem is. If I want to see the work on February 18, I get the result:
name. Work -------------------- Scott | Bugfix slayer | NULL Halpert |The reason for this is clear, Howard's work date is neither tap nor he is like 2014-02-18 ... What will be the best way to get the desired result?
I use MySQL and PHP.
(Sorry for the stupid title, I could not think anything better ..)
Add a filter to your filter:
SELECT e.name, add employees to t.task and e.name = T.name and t.date = DATE ('2012-02-18 ');
As your query this will return only to people who have no work, or the date of the specified work, it will leave those who have the work, but the specified date But not at Consider the result of not having your sample data included where segment:
empId | Name | Work | Date | ... ---------------------------------------- | 10 | Scott | Support | 2014-02-17 | 10 | Scott | Bugfix | 2014-02-18 | 11 | Schrute | Faucet Faucet 12 | Halpert | Bugfix | 2014-02-17 | 12 | Halpert | Development | 2014-02-18 | 13. Howard | Support | 2014-02-17 |
As you can see there is no record for Howard where
date = 2014-02-18, or where
date Zero, that's why no record for Howard has not come back. When you add to become your result, add the filter:
empId | Name | Work | Date | ... ---------------------------------------- | 10 | Scott | Bugfix | 2014-02-18 | 11 | Schrute | Faucet Faucet 12 | Halpert | Development | 2014-02-18 | 13. Howard | Faucet Faucet
Which I think is the desired result.
Comments
Post a Comment