I’m working on a little project, is sort like a agenda for technicians, each technician is assigned on a daily agenda.
I’m looking for a query that shows me only the technicians that hasn’t been assigned on a day in a branch (the company has two branches: East and West)
The query I’ve tried is:
SELECT *
FROM technicians
WHERE id_tech NOT
IN (
SELECT id_tech
FROM hours
)
AND branch = 'West'
This query returns me what I want, but I don’t know how to filter this with a date, I’ve tried many queries, and return me all the colums with duplicate results.
My tables are, the hours table where each tech has a task:
CREATE TABLE IF NOT EXISTS `hours` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_tech` int(11) NOT NULL,
`9_30` varchar(140) DEFAULT NULL,
`10_30` varchar(50) DEFAULT NULL,
`11_30` varchar(50) DEFAULT NULL,
`12_30` varchar(50) DEFAULT NULL,
`1_30` varchar(50) DEFAULT NULL,
`2_30` varchar(50) DEFAULT NULL,
`3_30` varchar(50) DEFAULT NULL,
`4_30` varchar(50) DEFAULT NULL,
`5_30` varchar(50) DEFAULT NULL,
`6_30` varchar(50) DEFAULT NULL,
`comments` varchar(50) DEFAULT NULL,
`date` text NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM ROW_FORMAT=DYNAMIC;
INSERT INTO `hours` (`id`, `id_tech`, `9_30`, `10_30`, `11_30`, `12_30`, `1_30`, `2_30`, `3_30`, `4_30`, `5_30`, `6_30`, `comments`, `date`) VALUES
(1, 1, 'Router with problems, Customer ID 111', 'Router with problems, Customer ID 111', 'Router with problems, Customer ID 111', 'Router with problems, Customer ID 111', 'Router with problems, Customer ID 111', 'Desktop with problems, Customer ID 121', NULL, NULL, NULL, 'Network problems, Customer ID 121', 'Router with problems, Customer ID 111', '16-07-2012'),
(3, 3, 'Network with problems, Customer ID 111', 'Network with problems, Customer ID 111', NULL, NULL, NULL, NULL, NULL, NULL, 'Network with problems, Customer ID 111', '', 'Didn''t came to work today', '16-07-2012');
And the technicians table:
CREATE TABLE IF NOT EXISTS `technicians` (
`id_tech` int(11) NOT NULL,
`name` varchar(50) COLLATE utf8_spanish_ci NOT NULL,
`branch` varchar(50) COLLATE utf8_spanish_ci NOT NULL,
PRIMARY KEY (`id_tech`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci ROW_FORMAT=DYNAMIC;
INSERT INTO `technicians` (`id_tech`, `name`, `branch`) VALUES
(1, 'Peter', 'East'),
(2, 'Juan', 'East'),
(3, 'Rick', 'West'),
(4, 'Mario', 'West');
Put the date comparison as one of the conditional checks in the
LEFT JOIN:^ This will get all technicians who do not have an assignment for the current day.
If you want to compare on a specific day in the past: