union 不返回重复行(所有字段值相同的行)
union all 返回所有行
// 每类最多统计100条
sql
select `server_id`,count(1) as logs from ( SELECT `server_id` FROM `log` WHERE `log`.`type` = "a" AND server_id=1 limit 100 )
UNION
select `server_id`,count(1) as logs from ( SELECT `server_id` FROM `log` WHERE `log`.`type` = "a" AND server_id=2 limit 100 )
UNION
select `server_id`,count(1) as logs from ( SELECT `server_id` FROM `log` WHERE `log`.`type` = "a" AND server_id=3 limit 100 );
sql
SELECT * FROM log WHERE type="a"
UNION
SELECT * FROM log WHERE type="b"
UNION
SELECT * FROM log WHERE type="c"
order by id desc LIMIT 10
【高性能mysql】
sql
(SELECT * FROM log WHERE type="a" order by id desc LIMIT 10)
UNION ALL
(SELECT * FROM log WHERE type="b" order by id desc LIMIT 10)
UNION ALL
(SELECT * FROM log WHERE type="c" order by id desc LIMIT 10)
order by id desc LIMIT 10
sql
select sum(num)
from
(
SELECT count(1) as num FROM log WHERE type="a"
UNION
SELECT count(1) as num FROM log WHERE type="b"
UNION
SELECT count(1) as num FROM log WHERE type="c"
)