Mysql
 sql >> Database >  >> RDS >> Mysql

mysql conta i record da due tabelle in una query?

Penso che questo dovrebbe funzionare supponendo che tsId e paId siano chiavi univoche:

SELECT Count(DISTINCT t.tsID) AS tsCount, 
    Count(DISTINCT p.paID) AS paCount
FROM account a 
    LEFT JOIN test t ON a.acId = t.tsAccountId
    LEFT JOIN patient p ON a.acId = p.paAccountId
WHERE a.acId = 1

Ed ecco SQL Fiddle .

Nota:il problema relativo alla mancata adesione alla tabella account (e al suo utilizzo come tabella master) è che se la tabella test o la tabella paziente non hanno dati per un ID account specifico, la query restituirà 0 risultati per ogni - - che potrebbe non essere corretto.