我有下表:
URL url = Thread.currentThread().getContextClassLoader().getResource("tourneeCollecteRawOutputCalculIndicateur.json");
File file = new File(url.getPath());
InputStream in = new FileInputStream(file)
Reader r = new InputStreamReader(in, StandardCharset.ISO_8859_1);
TourneeCollecteRawOutput tourneeCollecteRawOutput = mapper.readValue(in, mapper.getTypeFactory().constructCollectionType(List.class, TourneeCollecteRawOutput.class));
我在带有表A的Enterpr_id上有一个外键,我该如何进行计数转换并获得预期的输出:
Table A:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| ID | User | Enterpr_id |
| 1 | test1 | 1 |
| 2 | test2 | 2 |
| 3 | test3 | 3 |
| 4 | test4 | 4 |
| 5 | test5 | 1 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Table B:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Enterpr_id | Name |
| 1 | Nespresso |
| 2 | what |
| 3 | else |
| 4 | need |
| 5 | help |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
答案 0 :(得分:3)
这是一个简单的连接:
select a.user, b.name
from tablea a
inner join tableb b on b.entrepr_id = a.entrepr_id
编辑:从更新后的问题中,您似乎想要汇总和一个left join
:
select b.name, count(a.id) cnt_users
from tableb b
left join tablea a on a.entrepr_id = b.entrepr_id
group by b.entrepr_id, b.name
order by b.entrepr_id
答案 1 :(得分:0)
这是一个带有计数的左联接查询:
animalData