0

这是https://stackoverflow.com/questions/18487327/mysql-correct-approach-event-counting的后续问题

这是数据库表:

CREATE TABLE `event` (
  `event_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `visitor_id` int(11) DEFAULT NULL,
  `key` varchar(200) DEFAULT NULL,
  `value` text,
  `label` varchar(200) DEFAULT '',
  `datetime` datetime DEFAULT NULL,
  PRIMARY KEY (`event_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;


INSERT INTO `event` (`event_id`, `visitor_id`, `key`, `value`, `label`, `datetime`)
VALUES
    (1, 1, 'LOGIN', NULL, '', NULL),
    (2, 2, 'LOGIN', NULL, '', NULL),
    (3, 1, 'VIEW_PAGE', 'HOTEL', '', NULL),
    (4, 2, 'VIEW_PAGE', 'HOTEL', '', NULL),
    (5, 1, 'PURCHASE_HOTEL', NULL, '', NULL);

CREATE TABLE `visitor` (
  `visitor_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `datetime` datetime DEFAULT NULL,
  PRIMARY KEY (`visitor_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

INSERT INTO `visitor` (`visitor_id`, `datetime`)
VALUES
    (1, NULL),
    (2, NULL);

CREATE TABLE `attribute` (
  `attribute_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `visitor_id` int(11) DEFAULT NULL,
  `key` varchar(200) DEFAULT NULL,
  `value` text NOT NULL,
  `label` varchar(200) NOT NULL DEFAULT '',
  `datetime` datetime DEFAULT NULL,
  PRIMARY KEY (`attribute_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `attribute` (`attribute_id`, `visitor_id`, `key`, `value`, `label`, `datetime`)
VALUES
    (1, 1, 'TITLE', 'Professor', '', NULL);

我正在运行以下查询:

SELECT
    `e`.`visitor_id`
FROM 
    `event` e,
    `attribute` a
GROUP by 
    `e`.`visitor_id`
HAVING
    sum(e.key = 'LOGIN') > 0
AND sum(e.key = 'VIEW_PAGE' and e.value = 'HOTEL') > 0 
AND sum(e.key = 'PURCHASE_HOTEL') > 0
AND sum(a.key = 'TITLE' and a.value = 'Professor') > 0

我希望只返回具有 TITLE = 'Professor' 属性的访问者 (1),但不知何故,查询给了我两个访问者。

  • 我在这里做错了什么以便返回两个用户?
  • 如果他的条件不匹配(TITLE = 'Professor'),我用“AND”附加所有条件不应该排除访客2吗?
4

1 回答 1

2

您没有加入两个表:

SELECT
    `e`.`visitor_id`
FROM 
    `event` e join
    `attribute` a
    e.visitor_id = a.visitor_id
GROUP by 
    `e`.`visitor_id`
HAVING
    sum(e.key = 'LOGIN') > 0
AND sum(e.key = 'VIEW_PAGE' and e.value = 'HOTEL') > 0 
AND sum(e.key = 'PURCHASE_HOTEL') > 0
AND sum(a.key = 'TITLE' and a.value = 'Professor') > 0;

使用cross join(您的原始公式),所有属性都适用于所有事件。因此,所有事件都符合条件。

于 2013-08-28T14:23:14.437 回答