mysql中查询两个表同一条件的数据条数该怎么写sql语句

假如我想查表a和表b两个表里id>5的数据总数该怎么查
2024-10-29 06:33:43
推荐回答(4个)
回答1:

不知道你的a,b两表有没有关联,假定没有关联

select count(1)
from 
(
select id
from a 
where id>5
union all
select id 
from b 
where id>5
)

回答2:

select xxx as a,xxx as b,xxx as c from tbl_a
union
select xxx as a,xxx as b,xxx as c from tbl_b保证字段名相同,不够的用字符串顶替就OK了

回答3:

select * from a where id>5
union all
select * from b where id>5

回答4:

select count(1)
from
(
select id
from a
where id>5
union all
select id
from b
where id>5
)
AS `表名`