Reputation: 6436
I want to count how many visitors that are from Sweden (based on IP address) by using this SQL query:
SELECT COUNT(vd.data_country)
FROM visitors_details AS vd
JOIN visitors AS v
ON vd.id_visitor = v.id
WHERE v.id_website = '1'
AND vd.data_country = 'SE'
GROUP BY vd.id_visitor
The problem with this SQL query is that it shows 830 visitors that are from Sweden. When I count the Swedish visitors manually from the database, I'll get 671 visitors (I have marked every Swedish visitor in HeidiSQL so I have not miscounted).
If I change COUNT(vd.data_country)
to COUNT(DISTINCT vd.data_country)
it only shows 1 visitor.
Here's how the databases looks like:
CREATE TABLE IF NOT EXISTS `visitors` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_website` int(11) NOT NULL DEFAULT '0',
`data_ipaddress` text NOT NULL,
`data_useragent` text NOT NULL,
`data_referer` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
CREATE TABLE IF NOT EXISTS `visitors_details` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_visitor` int(11) NOT NULL,
`id_user` int(11) NOT NULL,
`id_permissions` int(11) NOT NULL,
`data_filename` text NOT NULL,
`data_filename_get` text NOT NULL,
`data_city` text NOT NULL,
`data_postalcode` bigint(20) NOT NULL,
`data_county` text NOT NULL,
`data_country` text NOT NULL,
`data_location` text NOT NULL,
`data_hostname` text NOT NULL,
`data_organisation` text NOT NULL,
`datetime_occurred` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
INSERT INTO `visitors` (`id`, `id_website`, `data_ipaddress`, `data_useragent`, `data_referer`)
VALUES(1, 1, '127.0.0.1', '', '')
INSERT INTO `visitors_details` (`id`, `id_visitor`, `id_user`, `id_permissions`, `data_filename`, `data_filename_get`, `data_city`, `data_postalcode`, `data_county`, `data_country`, `data_location`, `data_hostname`, `data_organisation`, `datetime_occurred`)
VALUES(1, 1, 0, 0, 'page-start.php', '-', 'city', 12345, 'county', 'SE', 'loc', 'hostname', 'org', '2015-03-31 18:45:37')
How can I solve this problem?
Upvotes: 0
Views: 53
Reputation: 781096
Use COUNT(DISTINCT id_visitor)
so you don't count a visitor separately for each file they access. And get rid of GROUP BY vd.id_visitor
.
COUNT(DISTINCT data_country)
is 1 because you've restricted it to just one country (Sweden).
Upvotes: 1