从SQL(MySQL)中的table1中的不同id计算table2中的distinct字段

时间:2011-03-24 07:44:21

标签: mysql join group-by distinct

我有两张桌子:

表1:

| order_id | shop_id | customer_id | total |  date    |
-------------------------------------------------------
|   9005   |    A    |      1      | 1205  | 20110210 |
|   9006   |    A    |      2      | 8591  | 20110212 |
|   9007   |    A    |      2      | 3472  | 20110216 |
|   9008   |    B    |      1      | 6310  | 20110218 |
-------------------------------------------------------

表2:

| shop_id | customer_id | reference |
-------------------------------------
|    A    |      1      | Friend    |
|    A    |      2      | Internet  |
|    B    |      1      | Friend    |
|    C    |      1      | Friend    |
-------------------------------------

我想从table1中选择不同的值(shop_id,customer_id)(按日期过滤),然后计算table2中的引用。

预期结果:

| reference | count |
---------------------
|  Friend   |   2   |
|  Internet |   1   |
----------------------

到目前为止,我正在使用的查询是:

SELECT引用,COUNT(*)作为计数FROM table1 JOIN table2 USING(shop_id,customer_id)GROUP BY引用

结果是:

| reference | count |
---------------------
|  Friend   |   2   |
|  Internet |   2   |
---------------------

问题在于计算2次:shop_id = A和customer_id = 2。这就是“互联网”被计算两次的原因。

有人可以帮我辨别出什么问题吗?如果可能的话,我希望在不使用子查询(技术限制)的情况下实现这一目标。

谢谢。

SQL Dump:

CREATE TABLE `table1` (
   `order_id` int(11),
   `shop_id` char(1),
   `customer_id` int(11),
   `total` smallint(6),
   `date` date
);

INSERT INTO `table1` (`order_id`, `shop_id`, `customer_id`, `total`, `date`) VALUES 
('9005', 'A', '1', '1205', '2011-02-10'),
('9006', 'A', '2', '8591', '2011-02-12'),
('9007', 'A', '2', '3472', '2011-02-16'),
('9008', 'B', '1', '6310', '2011-02-18');

CREATE TABLE `table2` (
   `customer_id` int(11),
   `shop_id` char(1),
   `reference` enum('Friend','Internet')
);

INSERT INTO `table2` (`customer_id`, `shop_id`, `reference`) VALUES 
('1', 'A', 'Friend'),
('2', 'A', 'Internet'),
('1', 'B', 'Friend'),
('1', 'C', 'Friend');

2 个答案:

答案 0 :(得分:1)

我认为这可能对你有用 - 至少它会返回预期的结果

SELECT reference,COUNT(distinct(concat(shop_id,'_',customer_id))) as count 
FROM table1 JOIN table2 USING(shop_id,customer_id) GROUP BY reference;

'_'是为了避免混合store_id和customer_id,你可以使用不同的散列函数来生成唯一的商店/客户ID

答案 1 :(得分:0)

这是一个使用子查询的解决方案:

SELECT reference, COUNT(*) as count FROM (SELECT shop_id,customer_id,reference FROM table1 JOIN table2 USING(shop_id,customer_id) GROUP BY shop_id,customer_id) as filtered GROUP BY(reference)

但我不想使用子查询...如果没有这样的替代方案,这将成为正确的答案。