select count(*) from ( select id from a union all select id from b union all select id from c union all select id from d) aa
select sum(t1.num) as '总记录数' from ( select count(*) as num from a union all select count(*) from b union all select count(*) from c union all select count(*) from d) t1
select (select count(*) as a from 表A)+ (select count(*) as a from 表B)+ (select count(*) as a from 表C)+ (select count(*) as a from 表D);
select count(1) from (select 1 from a union all select 1 from b union all select 1 from c union all select 1 from d ) as t
mysql> select * from aa; +------+---------+--------+ | id | content | parent | +------+---------+--------+ | 1 | AAAAAA | NULL | | 2 | BBBBBB | NULL | | 3 | cccccc | 2 | +------+---------+--------+ 3 rows in set (0.00 sec)mysql> select * from b; +-------+------+ | test | full | +-------+------+ | test1 | 50 | | test2 | 70 | | test3 | 100 | +-------+------+ 3 rows in set (0.00 sec)mysql> set @count=0; Query OK, 0 rows affected (0.02 sec)mysql> select count(*) into @count from aa; Query OK, 1 row affected (0.00 sec)mysql> select @count+count(*) from b; +-----------------+ | @count+count(*) | +-----------------+ | 6 | +-----------------+ 1 row in set (0.00 sec)
select id from a
union all
select id from b
union all
select id from c
union all
select id from d) aa
select count(*) as num from a
union all
select count(*) from b
union all
select count(*) from c
union all
select count(*) from d) t1
(select count(*) as a from 表B)+
(select count(*) as a from 表C)+
(select count(*) as a from 表D);
from
(select 1 from a
union all
select 1 from b
union all
select 1 from c
union all
select 1 from d
) as t
+------+---------+--------+
| id | content | parent |
+------+---------+--------+
| 1 | AAAAAA | NULL |
| 2 | BBBBBB | NULL |
| 3 | cccccc | 2 |
+------+---------+--------+
3 rows in set (0.00 sec)mysql> select * from b;
+-------+------+
| test | full |
+-------+------+
| test1 | 50 |
| test2 | 70 |
| test3 | 100 |
+-------+------+
3 rows in set (0.00 sec)mysql> set @count=0;
Query OK, 0 rows affected (0.02 sec)mysql> select count(*) into @count from aa;
Query OK, 1 row affected (0.00 sec)mysql> select @count+count(*) from b;
+-----------------+
| @count+count(*) |
+-----------------+
| 6 |
+-----------------+
1 row in set (0.00 sec)
如果表大了(单表超过100万),别这么干,那你服务器真要玩完了。这个时候你不如就先select,把结果插到一个临时表里,再sun一下就可以了。或者用存储过程写下就可以了