我有两张桌子EMAIL& EMAIL_MESSAGES。我试图找到未读消息的数量。
表EMAIL
CREATE TABLE IF NOT EXISTS EMAIL (
MAIL_NO TINYINT UNSIGNED NOT NULL,
BIZ_ID VARCHAR(35) CHARACTER SET 'latin1' COLLATE 'latin1_bin' NOT NULL,
FROM_ADD INT UNSIGNED NOT NULL, -- FOR EMAIL_TYPE EUCP, THIS WILL BE AGENT ID
TO_ADD INT UNSIGNED DEFAULT NULL, -- FOR EMAIL_TYPE EUCP, THIS WILL BE CANDIDATE_ID
EMAIL_SUBJECT VARCHAR(75) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' DEFAULT NULL,
MAIL_STATUS CHAR(1) CHARACTER SET 'latin1' COLLATE 'latin1_bin' DEFAULT 'A',
UPDATED_DATE DATETIME ON UPDATE CURRENT_TIMESTAMP,
EMAIL_TYPE VARCHAR(10) CHARACTER SET 'latin1' COLLATE 'latin1_bin' DEFAULT NULL,
PRIMARY KEY (MAIL_NO,BIZ_ID),
INDEX idx_EMAIL_id1 (FROM_ADD ASC),
INDEX idx_EMAIL_id2 (TO_ADD ASC),
INDEX idx_EMAIL_id3 (BIZ_ID ASC),
INDEX idx_EMAIL_id4 (MAIL_STATUS ASC),
INDEX idx_EMAIL_id5 (EMAIL_SUBJECT ASC),
INDEX idx_EMAIL_id6 (UPDATED_DATE ASC)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
表EMAIL_MESSAGE
CREATE TABLE IF NOT EXISTS EMAIL_MESSAGE (
MESSAGE_NO TINYINT UNSIGNED NOT NULL,
BIZ_ID VARCHAR(35) CHARACTER SET 'latin1' COLLATE 'latin1_bin' NOT NULL,
SENDER INT UNSIGNED DEFAULT NULL,
MESSAGE TEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' DEFAULT NULL,
ATTACHMENT VARCHAR(255) CHARACTER SET 'latin1' COLLATE 'latin1_bin' DEFAULT NULL,
MESSAGE_STATUS CHAR(1) CHARACTER SET 'latin1' COLLATE 'latin1_bin' DEFAULT 'U',
SEND_LOCATION VARCHAR(20) CHARACTER SET 'latin1' COLLATE 'latin1_bin' DEFAULT NULL,
READ_DATE DATETIME DEFAULT NULL,
SEND_DATE DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (MESSAGE_NO,BIZ_ID),
INDEX idx_EMAIL_MESSAGE_id1 (SENDER ASC),
INDEX idx_EMAIL_MESSAGE_id2 (MESSAGE_STATUS ASC),
CONSTRAINT fk_EMAIL_MESSAGE_C1
FOREIGN KEY (BIZ_ID)
REFERENCES EMAIL (BIZ_ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
样本数据
EMAIL:
INSERT INTO `EMAIL` (`MAIL_NO`,`BIZ_ID`,`FROM_ADD`,`TO_ADD`,`EMAIL_SUBJECT`,`MAIL_STATUS`,`UPDATED_DATE`,`EMAIL_TYPE`) VALUES (1,'10001-10001-10001',10001,10002,'SUBJECT ABCD','A','2015-09-23 15:14:45','EUCP');
EMAIL_MESSAGE
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (1,'10001-10001-10001',10001,'check this out',NULL,'R','TBD','2015-09-23 16:04:32','2015-07-07 19:40:12');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (2,'10001-10001-10001',10001,'Good man\n\nJamo here',NULL,'R','TBD','2015-09-23 16:04:32','2015-07-07 19:42:32');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (3,'10001-10001-10001',10001,'Get back to me when you can',NULL,'R','TBD','2015-09-23 16:04:32','2015-07-07 20:04:56');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (4,'10001-10001-10001',10002,'COOL',NULL,'R','TBD','2015-09-23 16:04:32','2015-07-07 20:41:48');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (5,'10001-10001-10001',10002,'good \n',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-19 18:55:36');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (6,'10001-10001-10001',10002,'Hoi\n',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-19 19:27:13');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (7,'10001-10001-10001',10001,'Good man',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-21 11:22:33');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (8,'10001-10001-10001',10002,'THANKS',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-21 12:05:38');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (9,'10001-10001-10001',10002,'How are you john?',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-23 13:55:19');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (10,'10001-10001-10001',10002,'Good',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-23 13:55:55');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (11,'10001-10001-10001',10001,'Thanks you',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-23 15:07:12');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (12,'10001-10001-10001',10002,'Where is the opportunity',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-23 15:16:12');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (13,'10001-10001-10001',10001,'Leeds\n',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-23 15:16:49');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (14,'10001-10001-10001',10001,'Interested?',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-23 15:20:59');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (15,'10001-10001-10001',10002,'sdg',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-23 15:25:48');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (16,'10001-10001-10001',10001,'asdf',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-23 15:26:12');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (17,'10001-10001-10001',10002,'asdf',NULL,'R','TBD','2015-09-23 16:04:32','2015-09-23 15:50:32');
INSERT INTO `EMAIL_MESSAGE` (`MESSAGE_NO`,`BIZ_ID`,`SENDER`,`MESSAGE`,`ATTACHMENT`,`MESSAGE_STATUS`,`SEND_LOCATION`,`READ_DATE`,`SEND_DATE`) VALUES (18,'10001-10001-10001',10001,'SAF',NULL,'U','TBD',NULL,'2015-09-23 16:04:34');
工作场景:e.FROM_ADD有未读消息的场景
Select e.MAIL_NO, e.BIZ_ID, e.FROM_ADD AS AGENT_ID, e.TO_ADD AS CANDIDATE_ID, e.EMAIL_SUBJECT, e.MAIL_STATUS,
em.SENDER, IF(em.UNREAD_MESSAGE_COUNT IS NULL,0,em.UNREAD_MESSAGE_COUNT) as UNREAD_MESSAGE_COUNT From EMAIL e left join
(Select COUNT(*) as UNREAD_MESSAGE_COUNT, SENDER, BIZ_ID FROM EMAIL_MESSAGE Inner Join EMAIL Using (BIZ_ID)
where MESSAGE_STATUS = 'U' and SENDER not in ('10001') group by SENDER) em
on em.BIZ_ID = e.BIZ_ID where
e.FROM_ADD='10001'
失败的场景:e.TO_ADD有未读消息的场景
Select e.MAIL_NO, e.BIZ_ID, e.FROM_ADD AS AGENT_ID, e.TO_ADD AS CANDIDATE_ID, e.EMAIL_SUBJECT, e.MAIL_STATUS,
em.SENDER, IF(em.UNREAD_MESSAGE_COUNT IS NULL,0,em.UNREAD_MESSAGE_COUNT) AS UNREAD_MESSAGE_COUNT From EMAIL e Left Join
(Select COUNT(*) AS UNREAD_MESSAGE_COUNT, SENDER, BIZ_ID From EMAIL_MESSAGE Inner Join EMAIL Using (BIZ_ID)
where MESSAGE_STATUS = 'U' and SENDER not in ('10002') group by SENDER) em
on e.BIZ_ID = em.BIZ_ID
Where
e.TO_ADD="10002";
由于某些原因,相同的查询在一个场景中失败。我无法弄清楚为什么失败场景的左连接会产生空值?非常感谢任何帮助!