阅读量:4
union 不返回重复行(所有字段值相同的行)
union all 返回所有行
// 每类最多统计100条
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 );
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】
(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
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" )