-1

我有两张表 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

什么是正确的语法?

4

3 回答 3

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。

于 2013-05-29T17:19:37.520 回答
1

错误信息是:

Operand should contain 1 column(s)

发生这种情况是因为您的子查询:

Select * From Call_Center_Log where Customer_ID = Customer.id AND Date_Done >= curdate()

返回所有字段。

要修复它,只需更改*Customer_ID

http://sqlfiddle.com/#!2/5da69/9

于 2013-05-29T17:20:24.817 回答
1

您在 Stackoverflow 和 sqlfiddle 中的查询是不同的。

你写"Select * from call_center_log"而不是"select customer_id from call_center_log"

于 2013-05-29T17:20:35.387 回答