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

Indlejrede forespørgsler for at få optælling med to betingelser

Du skal gruppere kolonne infection og (ip &ipc ) anderledes, så tilslut dem ved at bruge en underforespørgsel som denne:

SELECT t1.ip, t1.isp, t2.infection, t1.ipc, t1. ispc, t2.incount FROM (SELECT ip, isp, infection, COUNT(ip) as ipc, COUNT(isp) as ispc FROM ( SELECT ip, isp, infection FROM tbl1 UNION ALL SELECT ip, isp, infection FROM tbl2 UNION ALL SELECT ip, isp, infection FROM tbl3 )x GROUP BY ip, isp) t1 JOIN (SELECT ip, isp, infection, COUNT(infection) as incount FROM ( SELECT ip, isp, infection FROM tbl1 UNION ALL SELECT ip, isp, infection FROM tbl2 UNION ALL SELECT ip, isp, infection FROM tbl3 )x GROUP BY ip, isp, infection)t2 ON t1.ip = t2.ip ORDER BY ip, isp, infection Desc

Se denne SQLFiddle

Bemærk: Jeg tror, ​​at dit ønskede output er forkert, fordi:

  1. I Table3 der er ingen infection for ip=6 men det er i dit output
  2. infection other mangler i dit output (i stedet er der malware )


  1. MySQL UNIX_TIMESTAMP til PostgreSQL

  2. få værdi fra MySQL-database med PHP

  3. PDO-forbindelse til MySQL-database afvist

  4. Adgang til URL-parametre i Oracle Forms / OC4J