我有两个表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
当我必须弄清楚上述标准以及我需要排除任何已经被称为今天的客户时,我的打嗝就会出现。这将留下我只记录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
正确的语法是什么?
答案 0 :(得分:3)
你的语法非常接近你的小提琴:
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 Customer.id NOT IN
(SELECT Customer_Id
FROM Call_Center_Log
WHERE Customer_ID = Customer.id
AND Date_Done >= curdate())
ORDER BY Priority DESC
只需要select customer_id
代替select *
- 然后它返回记录1.
答案 1 :(得分:1)
错误消息是:
Operand should contain 1 column(s)
这是因为您的子查询:
Select * From Call_Center_Log where Customer_ID = Customer.id AND Date_Done >= curdate()
返回所有字段。
要解决此问题,只需将*
更改为Customer_ID
:
答案 2 :(得分:1)
您在Stackoverflow和sqlfiddle中的查询是不同的。
你写了"Select * from call_center_log"
而不是"select customer_id from call_center_log"