Question

I have a large log of authentication between various networks that I need to summarize.

First I run

SELECT Home, Con, COUNT(*) AS 'ACCEPTS' FROM AUTHLOG WHERE response='ACCEPT';

Which gives me the summary:

+------+------+---------+
| Home | Con  | ACCEPTS |
+------+------+---------+
| net1 | net1 |      37 |
| net1 | net2 |       2 |
| net1 | net3 |     578 |
| net2 | net1 |      56 |
| net2 | net2 |   95621 |
| net2 | net3 |     465 |
| net3 | net1 |      91 |
| net3 | net2 |      83 |
| net3 | net3 |     891 |
+------+------+---------+

I need to start storing these results in a separate table, accumulating the total ACCEPTS (since the AUTHLOG table gets truncated).

The seperate summary table is very simple:

CREATE TABLE ACCEPTS_SUMMARY (
   Home char(50) DEFAULT NULL,
   Con char(50) DEFAULT NULL,
   Accepts INT UNSIGNED DEFAULT 0
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Is there a way, within MySQL preferably (no Python or Perl on the host, a bash script could work though), that I could get the above counts, and add them to the existing totals in a summary table, or insert if the Home and Con combination doesn't already exist.

Was it helpful?

Solution

This should do the trick:

INSERT INTO summary(Home, Con, ACCEPTS)
SELECT Home, Con, COUNT(*)
FROM AUTHLOG
WHERE response='ACCEPT'
ON DUPLICATE KEY UPDATE ACCEPTS = ACCEPTS + VALUES(ACCEPTS)

Make sure table summary has a UNIQUE INDEX on (Home, Con).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top