我有两张桌子。我想数一数一个值在另一个表中的次数。因此,表"sorgente“中的密码在table contatore中是不同的,因为我在代码前面有'BRANO-‘后缀。我试过喜欢,但不起作用。
sorgente
| codice | nome |
| 15 | mario |
| 16 | mary |
contatore
| nome_evento | data |
| BRANO-15 | 2020-08-15 |
| BRANO-15 | 2020-08-16 |
| BRANO-16 | 2020-08-14 |因此,查询可能是
SELECT sorgente.codice, count(contatore.nome_evento)
FROM sorgente
JOIN contatore
WHERE contatore.nome_evento LIKE '%-'sorgente.codice但我有个错误的结果
发布于 2020-08-17 22:59:46
使用字符串连接。子查询似乎是一个自然的解决方案:
select
s.codice,
(
select count(*)
from contatore c
where c.nome_evento = concat('BRANO-', s.codice)
) no_matches_in_contatore
from sorgente s但是,您也可以加入和聚合:
select s.codice, count(c.nome_evento) no_matches_in_contatore
from sorgente s
left join contatore c on c.nome_evento = concat('BRANO-', s.codice)
group by s.codicehttps://stackoverflow.com/questions/63459675
复制相似问题