sql语句 table_1中的A列中的字段,在table_2中的A列查出现次数,并在 table_1中的B列对应填充,怎么写
sql语句table_1中的A列中的字段,在table_2中的A列查出现次数,并在table_1中的B列对应填充,怎么写?table_1:ABaceftable_2:Aa...
sql语句 table_1中的A列中的字段,在table_2中的A列查出现次数,并在 table_1中的B列对应填充,怎么写?
table_1:
A B
a
c
e
f
table_2:
A
a
a
a
b
c
c
d
e
f
f
f
f
g
h
h
结果在table_1中应展示为:
A B
a 3
c 2
e 1
f 4 展开
table_1:
A B
a
c
e
f
table_2:
A
a
a
a
b
c
c
d
e
f
f
f
f
g
h
h
结果在table_1中应展示为:
A B
a 3
c 2
e 1
f 4 展开
1个回答
2018-01-20
展开全部
sqlserver
CREATE TABLE a(NAME VARCHAR(50)
CREATE TABLE b(NAME VARCHAR(50)
INSERT INTO a
SELECT 'A B' UNION ALL
SELECT 'a' UNION ALL
SELECT 'c' UNION ALL
SELECT 'e' UNION ALL
SELECT 'f'
INSERT INTO b
SELECT 'A' UNION ALL
SELECT 'a' UNION ALL SELECT 'a' UNION ALL SELECT 'a' UNION ALL
SELECT 'b' UNION ALL
SELECT 'c' UNION ALL SELECT 'c' UNION ALL
SELECT 'd' UNION ALL
SELECT 'e' UNION ALL
SELECT 'f' UNION ALL SELECT 'f' UNION ALL SELECT 'f' UNION ALL SELECT 'f' UNION ALL
SELECT 'g' UNION ALL
SELECT 'h' UNION ALL SELECT 'h'
SELECT * FROM a;
SELECT * FROM b;
SELECT a.NAME , t.num FROM a LEFT JOIN (
SELECT name,COUNT(*) num FROM b GROUP BY name
) t ON a.NAME =t.NAME
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询