我有2个问题:
第一次查询:
select TableB.name, count(TableB.name) from TableB group by TableB.name
结果:
name | count
Jack | 2
Marry| 1
和
第二次查询:
select DISTINCT TableA.kName, TableA.Value from TableA inner join TableB
ON
TableA.kName=TableB.name
结果:
kName | Value
Jack | 1
Marry | 3
但我需要结果表:
kName | Value | newColumn
Jack | 1 | 2
Marry | 3 | 1
其中newColumn是第一次查询的结果,怎么办呢?求助。
也许
select DISTINCT TableA.kName, TableA.value,
(select TableB.name, count(TableB.name) from TableB group by TableB.name)
AS
newColumn from TableA inner join TableB ON TableA.kName=TableB.name
但这不起作用:(
答案 0 :(得分:3)
尝试此查询
SELECT tableB.name, tableA.value, count(tableB.name) as newColum FROM tableB
JOIN tableA ON tableB.name = tableA.kname
GROUP BY tableB.name,tableA.value
答案 1 :(得分:-1)
select kname, value, "newColumn"
from
(
select name, count(name) as "newColumn"
from tb
group by name
) tb
inner join
(
select distinct kname, value
from ta
) ta ta.kname = tb.name