我有两张表 Customers 和 Call_center_logs。
这是 SQL Sqlfiddle 在这里: http ://sqlfiddle.com/#!2/5da69/3
CREATE TABLE `Call_Center_Log` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Customer_ID` int(11) DEFAULT NULL,
`Date_Done` date DEFAULT NULL,
PRIMARY KEY (`ID`));
CREATE TABLE `Customer` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Criteria` varchar(45) DEFAULT NULL,
`Criteria1` int(11) DEFAULT NULL,
`Criteria2` int(11) DEFAULT NULL,
`Criteria3` int(11) DEFAULT NULL,
`Ctiteria4` varchar(45) DEFAULT NULL,
`state` varchar(45) DEFAULT NULL,
`last_name` varchar(45) DEFAULT NULL,
`Priority` int(11) DEFAULT NULL,
`first_name` varchar(45) DEFAULT NULL,
`company_id` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`));
INSERT INTO `Customer` (`Criteria`, `Criteria1`, `Criteria2`, `Criteria3`, `state`, `last_name`, `Priority`, `first_name`, `company_id`) VALUES ('Gold', '10001', '999998', '1001', 'CT', 'Smith', '5', 'Jim', '1');
INSERT INTO `Customer` (`Criteria`, `Criteria1`, `Criteria2`, `Criteria3`, `state`, `last_name`, `Priority`, `first_name`, `company_id`) VALUES ('Gold', '10001', '999998', '1001', 'ME', 'Jones', '9', 'Tom', '2');
INSERT INTO `Customer` (`Criteria`, `Criteria1`, `Criteria2`, `Criteria3`, `state`, `last_name`, `Priority`, `first_name`,`company_id`) VALUES ('Silver', '9999', '1000000', '999', 'TX', 'Tanaka', '3', 'Yoshi', '3');
INSERT INTO `Customer` (`Criteria`, `Criteria1`, `Criteria2`, `Criteria3`, `state`, `last_name`, `Priority`, `first_name`,`company_id`) VALUES ('Gold', '10001', '999998', '1001', 'CT', 'Blake', '13', 'Tom', '1');
INSERT INTO `Customer` (`Criteria`, `Criteria1`, `Criteria2`, `Criteria3`, `state`, `last_name`, `Priority`, `first_name`,`company_id`) VALUES ('SIlver', '9999', '1000000', '999', 'TX', 'Nakamoto', '3', 'Kesuke', '1');
INSERT INTO `Call_Center_Log` (`Customer_ID`, `Date_Done`) VALUES ('2', '2013-05-29');
INSERT INTO `Call_Center_Log` (`Customer_ID`, `Date_Done`) VALUES ('3', '2013-05-29');
INSERT INTO `Call_Center_Log` (`Customer_ID`, `Date_Done`) VALUES ('4', '2013-05-29');
INSERT INTO `Call_Center_Log` (`Customer_ID`, `Date_Done`) VALUES ('5', '2013-04-01');
我需要找出客户满足以下标准:(这很好用)
SELECT *
FROM Customer
WHERE Criteria= 'Gold'
AND Criteria1 > 10000
AND Criteria2 < 999999
AND Criteria3 >1000
AND state IN('CT','ME','IL','NV')
AND (Ctiteria4 is null OR Ctiteria4 ='')
Order by Priority DESC
它给了我记录
ID Last Name etc....
4 Blake
2 Jones
1 Smith
当我必须弄清楚上述标准时,我的 Hiccup 就会出现,而且我需要排除今天已经被呼叫的任何客户。这会让我简单地记录 ID 1/史密斯。
正如您在下面看到的,我正在摸索NOT IN。
SELECT *
FROM Customer
WHERE Criteria= 'Gold'
AND Criteria1 > 10000
AND Criteria2 < 999999
AND Criteria3 >1000
AND state IN('CT','ME','IL','NV')
AND (Ctiteria4 is null OR Ctiteria4 ='')
--> AND ID not in (Select Customer_ID FROM Call_Center_Log WHERE Customer_ID = id AND Taked_to_date > CURDATE())
Order by Priority DESC
什么是正确的语法?