SELECT id FROM
(SELECT from_id as id FROM email
union ALL
SELECT to_id as id FROM email) as tb_email GROUP by id HAVING COUNT(id)>100;

解决方案 »

  1.   

    不行,返回如下错误:
    You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT from_id as id FROM `email` union ALL SELECT to_id as id
      

  2.   

    你的mysql版本是多少?是不是不支持子查询?我这里4.1执行没有问题。
      

  3.   

    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2 to server version: 4.0.17-nt
      

  4.   

    我是真不知道我数据库的版本啊,因为我才刚刚开始接触这个数据库!
    我在PHP中用phpinfo()看了一下,数据库可能是3.23.49 版本的如果我的数据库不支持子查询,该怎么完成我需要的查询?
      

  5.   

    分别统计发信和收信
    SELECT count(*), from_id FROM email GROUP BY from_id;
    SELECT count(*), to_id FROM email GROUP BY to_id;
      

  6.   

    有没有办法合并啊,这样的话,我当然可以在程序中实现统计功能,但是如果用一条SQL语句是不是可以大大提高运行效率啊?
      

  7.   

    SSLECT id
      FROM email
     WHERE SELECT count(*), from_id FROM email GROUP BY from_id  AND
           SELECT count(*), to_id FROM email GROUP BY to_id;