我是SQL的新手,我有两个这样的表
tab1:
+---+---+
| ID|som|
+---+---+
| e| 1|
| d| j|
| c| 1|
| b| 1|
| a| p|
+---+---+
tab2:
+------+---+
|SK_CUR|som|
+------+---+
| b| d|
| a| c|
| a| i|
+------+---+
,我只想计算两个表中字母的出现。因此输出应如下所示:
+------+---+
| ID| oc|
+------+---+
| a| 3|
| b| 2|
| c| 1|
| d| 1|
| e| 1|
+------+---+
答案 0 :(得分:2)
我认为您只想要union all
和group by
:
select id, count(*)
from ((select id from t1) union all
(select SK_CUR from t2)
) t
group by id;
答案 1 :(得分:1)
尝试以下查询,
select ID , count(*) from (
select ID from tab1 where like '%[a-z]%'
union all
select som as ID from tab1 where som like '%[a-z]%'
union all
select SK_CUR as ID from tab2 where SK_CUR like '%[a-z]%'
union all
select som as ID from tab2 where som like '%[a-z]%')
group by ID
由于您只需要两个表的所有列中的字母,因此我使用了过滤器 就像“%[a-z]%”