表结构如下:
id Mail_To Mail_From Rsflg Rsdate主键 收件人(Email地址) 发件人(Email地址) 收发标志(R:收S:发) 收发时间
需求:
一个Email地址在一段时间内接受到的邮件数量以及发出去的数量。

解决方案 »

  1.   

    查询出来的结果如下:
    email  s_count r_count 
    [email protected]   1    3不能像:
    email  s_count r_count 
    [email protected]   1    0
    [email protected]   0    3
      

  2.   


       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  3.   

    DROP TABLE IF EXISTS `b_mail`;
    CREATE TABLE `b_mail` (
      `id` bigint(19) NOT NULL AUTO_INCREMENT,
      `User_Id` varchar(50) NOT NULL,
      `Mail_To` varchar(2000) NOT NULL,
      `Folder` varchar(20) NOT NULL,
      `Mail_From` varchar(200) NOT NULL,
      `Rsflg` char(1) DEFAULT NULL,
      `Cc` varchar(2000) DEFAULT NULL,
      `Bcc` varchar(2000) DEFAULT NULL,
      `Replyto` varchar(60) DEFAULT NULL,
      `Replyflg` char(1) DEFAULT NULL COMMENT '针对收进来的邮件Y:已回复,N:未回复。',
      `Subject` varchar(255) DEFAULT NULL,
      `Body` longtext,
      `Filenam` varchar(2000) DEFAULT NULL,
      `Size` decimal(16,0) DEFAULT NULL,
      `Rsdate` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
      `Delflg` char(1) DEFAULT 'N',
      `Audflg` char(1) DEFAULT NULL,
      `Msg_id` varchar(120) DEFAULT NULL,
      `Strangeflag` char(1) DEFAULT NULL,
      `Sendstatus` char(1) DEFAULT NULL,
      `Sendcount` int(10) DEFAULT NULL,
      `Needrtflag` char(1) DEFAULT NULL,
      `Tasknum` varchar(30) DEFAULT NULL,
      `Plansenddate` datetime DEFAULT NULL,
      `Rubmailflg` char(1) DEFAULT 'N',
      `Readflg` char(1) DEFAULT NULL,
      `Priority` char(1) DEFAULT NULL,
      `Fromip` varchar(100) DEFAULT NULL,
      `Importflg` char(1) DEFAULT NULL,
      `Forwardflg` char(1) DEFAULT 'N',
      `Aimflg` int(4) DEFAULT '0' COMMENT '联系目的',
      `Disflg` char(1) DEFAULT 'N',
      `Re` varchar(400) DEFAULT NULL,
      `Auditor` varchar(50) DEFAULT NULL COMMENT '审核人',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=651 DEFAULT CHARSET=utf8;
      

  4.   

    INSERT INTO `b_mail` VALUES ('649', 'wangkj', '[email protected]', '已发送邮件', '[email protected]', 'S', '', '', '', null, '公告', '​公告tyytuu', '20120204\\销售机会表.txt', '0', '2012-02-04 11:34:49', 'N', 'Y', null, '', null, '0', null, null, null, 'N', null, null, null, null, 'N', '0', 'N', null, 'wangkj');
    INSERT INTO `b_mail` VALUES ('650', 'wangkj', '[email protected]', '已发送邮件', '[email protected]', 'S', '', '', '', null, '公告', '​公告f附件修改成功!!!!!!!', '20120204\\201202041345750\\新建 文本文档.txt*20120204\\201202041345046\\新建 文本文档.txt', '0', '2012-02-04 13:41:47', 'N', 'Y', null, '', null, '0', null, null, null, 'N', null, null, null, null, 'N', '0', 'N', null, 'wangkj');
      

  5.   

    结果 如下:
    email(邮件地址)  s_count(发送的数量) r_count(接受的数量)
    '[email protected]' 1                     3mysql 5.1
      

  6.   

    mysql> select * from b_mail;
    +-----+---------+------------------+------------+----------------------+-------+------+------+---------+----------+---------+----------------------------------+---------------------------------------------------------------------------------------+------+---------------------+--------+--------+--------+-------------+------------+-----------+------------+---------+--------------+------------+---------+----------+--------+-----------+------------+--------+--------+--------+---------+
    | id  | User_Id | Mail_To          | Folder     | Mail_From            | Rsflg | Cc   | Bcc  | Replyto | Replyflg | Subject | Body                             | Filenam                                                                               | Size | Rsdate              | Delflg | Audflg | Msg_id | Strangeflag | Sendstatus | Sendcount | Needrtflag | Tasknum | Plansenddate | Rubmailflg | Readflg | Priority | Fromip | Importflg | Forwardflg | Aimflg | Disflg | Re | Auditor |
    +-----+---------+------------------+------------+----------------------+-------+------+------+---------+----------+---------+----------------------------------+---------------------------------------------------------------------------------------+------+---------------------+--------+--------+--------+-------------+------------+-----------+------------+---------+--------------+------------+---------+----------+--------+-----------+------------+--------+--------+--------+---------+
    | 649 | wangkj  | [email protected] | 已发送邮件 | [email protected] | S     |      |      |         | NULL     | 公告    | ?公告tyytuu                      | 20120204\销售机会表.txt                                                               |    0 | 2012-02-04 11:34:49 | N      | Y      | NULL   |             | NULL       |         0 | NULL       | NULL    | NULL         | N          | NULL   | NULL     | NULL   | NULL      | N          |      0 | N      | NULL   | wangkj  |
    | 650 | wangkj  | [email protected] | 已发送邮件 | [email protected] | S     |      |      |         | NULL     | 公告    | ?公告f附件修改成功!!!!!!! | 20120204\201202041345750\新建 文本文档.txt*20120204\201202041345046\新建 文本文档.txt |    0 | 2012-02-04 13:41:47 | N      | Y      | NULL   |             | NULL       |         0 | NULL       | NULL    | NULL         | N          | NULL   | NULL     | NULL   | NULL      | N          |      0 | N      | NULL   | wangkj  |
    +-----+---------+------------------+------------+----------------------+-------+------+------+---------+----------+---------+----------------------------------+---------------------------------------------------------------------------------------+------+---------------------+--------+--------+--------+-------------+------------+-----------+------------+---------+--------------+------------+---------+----------+--------+-----------+------------+--------+--------+--------+---------+
    2 rows in set (0.05 sec)
    mysql>通过以上记录,你的结果怎么得到的?!'[email protected]' 1                     3 这个1和3是怎么得到的?!无法理解。
      

  7.   

    select t_R.R_stat,t_S.S_stat,Mail_To from 
    (select count(1) as R_stat,Mail_To from table where  Rsdate>2011-1-1  and Rsflg='R'   group by  Mail_To) as t_R
     
    left join (select count(1) as S_stat, Mail_From  from table where  Rsdate>2011-1-1  and Rsflg='S'   group by   Mail_From ) as t_Son t_R.Mail_To = t_S.Mail_From