pageview snippets

How to use MySQL's ON DUPLICATE to track page views

Tagged mysql, duplicate, pageview, analytics  Languages sql

With MySQL 5 you can track page views with ON DUPLICATE.

The table:

create table page_views(
  uri varchar(500) not null,
  views int(11) not null default '0',
  primary key(uri)

The SQL:

insert into page_views (uri, views) values ('/products', 1) on duplicate key update views = views + 1;

See for more information.