从MySQL / oracle中的多个表计数

时间:2018-04-12 04:20:10

标签: mysql sql oracle hibernate

有4个表:A,B,C,D
有一个id:4(输入)
查询应该打印表中存在id的次数

EX:

in table A id:4 occurs 5 times
in table B id:4 occurs 7 times
in table C id:4 occurs 3 times
in table D id:4 occurs 1 times

输出:

Table name       No of occurence

A                   5
B                   7
C                   3
D                   1

3 个答案:

答案 0 :(得分:2)

你可以尝试这样的事情:

select 'A', count(*) from a where id = 4
union all
select 'B', count(*) from b where id = 4
union all
select 'C', count(*) from c where id = 4
union all
select 'D', count(*) from d where id = 4

答案 1 :(得分:0)

执行union all

select 'A' as Name, count(*) as occurence from tablea where id = ?
union all
select 'B' as Name, count(*) as occurence from tableb where id = ?
union all
select 'C' as Name, count(*) as occurence from tablec where id = ?
union all
select 'D' as Name, count(*) as occurence from tabled where id = ?

答案 2 :(得分:0)

select id as Name,count(id) as occurence from tableA where id = 4 group by id
union all
select id as Name,count(id) as occurence from tableB where id = 4 group by id
union all
select id as Name,count(id) as occurence from tableC where id = 4 group by id
union all
select id as Name,count(id) as occurence from tableD where id = 4 group by id