mysql/oracle中多表的计数

6pp0gazn  于 2021-06-24  发布在  Mysql
关注(0)|答案(3)|浏览(265)

有4个tables:a、b、c、d
有一个id:4(输入)
查询应该打印出id出现在表中的次数
前任:

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
ltqd579y

ltqd579y1#

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
vwoqyblh

vwoqyblh2#

你可以试试这样的方法:

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
irtuqstp

irtuqstp3#

做那件事 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 = ?

相关问题