SQL 查询计数的数据多于现有数据
SQL query counts more data than it exists
我想通过使用此 SQL 查询来统计有多少来自瑞典的访问者(基于 IP 地址):
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
此 SQL 查询的问题在于它显示了来自瑞典的 830 位访问者。当我从数据库中手动统计瑞典访客时,我会得到 671 名访客(我在 HeidiSQL 中标记了每个瑞典访客,所以我没有算错)。
如果我将 COUNT(vd.data_country)
更改为 COUNT(DISTINCT vd.data_country)
,它只会显示 1 个访问者。
数据库如下所示:
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')
我该如何解决这个问题?
使用 COUNT(DISTINCT id_visitor)
这样您就不会为访问者访问的每个文件分别计算访问者。并摆脱 GROUP BY vd.id_visitor
.
COUNT(DISTINCT data_country)
是 1,因为您将其限制在一个国家(瑞典)。
我想通过使用此 SQL 查询来统计有多少来自瑞典的访问者(基于 IP 地址):
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
此 SQL 查询的问题在于它显示了来自瑞典的 830 位访问者。当我从数据库中手动统计瑞典访客时,我会得到 671 名访客(我在 HeidiSQL 中标记了每个瑞典访客,所以我没有算错)。
如果我将 COUNT(vd.data_country)
更改为 COUNT(DISTINCT vd.data_country)
,它只会显示 1 个访问者。
数据库如下所示:
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')
我该如何解决这个问题?
使用 COUNT(DISTINCT id_visitor)
这样您就不会为访问者访问的每个文件分别计算访问者。并摆脱 GROUP BY vd.id_visitor
.
COUNT(DISTINCT data_country)
是 1,因为您将其限制在一个国家(瑞典)。