在 SQL (MySQL) 中根据表 1 中的不同 ID 计算表 2 中的不同字段

发布于 2024-10-26 05:16:07 字数 2105 浏览 3 评论 0原文

我有 2 个表:

表 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 reference,COUNT(*) as count FROM table1 JOIN table2 USING(shop_id,customer_id) GROUP BY reference

结果是:

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

问题是计数了 2 次:shop_id=A 和 customer_id=2。这就是为什么“互联网”被计算两次。

有人可以帮我找出问题所在吗?如果可能的话,我想在不使用子查询的情况下实现这一点(技术限制)。

谢谢。

SQL 转储:

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');

I have 2 tables:

Table 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 |
-------------------------------------------------------

Table 2:

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

I want to select distinct values (shop_id, customer_id) from table1 (filtered by date) and then count the references at table2.

Expected result:

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

So far, the Query that I'm using is:

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

And the result is:

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

The problem is that is counting 2 times: shop_id=A and customer_id=2. That is why "Internet" is counted twice.

Could someone help me to identify what is wrong? If possible, I would like to achieve this without using sub-queries (technical limitation).

Thank you.

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');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤云独去闲 2024-11-02 05:16:07

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

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

I think this might work for you - at least it returns the expected result

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

The '_' is to avoid mixing store_id and customer_id, you can probably use a different hashing function to generate unique store/customer ID

人间☆小暴躁 2024-11-02 05:16:07

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

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)

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

This is one solution using sub-queries:

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)

But I would prefer not using sub-queries... If there is not such an alternative, this will become the correct answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文