Mysql分组与计数

发布于 2024-11-26 06:34:51 字数 1541 浏览 5 评论 0原文

我有 3 个表:

1 TableTraders
trader_id|Domain|
---------------------
1|google.com
2|yahoo.com

2 个表 Counter_in

counter_time    counter_ip  counter_domain_id
    111111  222222  1
    111111  222224  2
    111111  222225  3
    111111  222232  2
    111111  222221  3
    111111  222223  4
    1111311     22223422    5



 3 Table Out Counter
counter_time    counter_ip  counter_domain_id
    111111  222222  1
    111111  222222  2
    111111  222222  3
    111111  222226  2
    111111  222221  3
    111111  222222  4
1309351485  2130708581  5
    1309351485  2130708581  4
    1309710116  2130706433  4
    1309351485  2130708581  1
1309710274  2130706433  1

好的,我想要做的是将表 1 、 2 、 3 连接起来,并将它们分组在一起,然后对它们进行计数。

trader_id|DOMAIN|IN|OUT
--------------------------
1|google.com|3|1
2|yahoo.com|1|2

我尝试了很多事情,但总是得到错误的结果。我如何正确加入查询以获得我想要的结果

*编辑

尝试的查询:

SELECT traders.trader_id, traders.domain, COUNT( counter_in.counter_domain_id ) AS today_in, COUNT( counter_out.counter_domain_id ) AS today_out
FROM traders
JOIN counter_in ON traders.trader_id = counter_in.counter_domain_id
JOIN counter_out ON traders.trader_id = counter_out.counter_domain_id
GROUP BY traders.trader_id, traders.domain
LIMIT 0 , 30

结果:

trader_id   domain  today_in    today_out
1   bing.com    3   3
2   google.com  4   4
3   yahoo.com   4   4
4   msn.com     3   3
5   yandex.com  1   1

i have a 3 Tables:

1 TableTraders
trader_id|Domain|
---------------------
1|google.com
2|yahoo.com

2 Table Counter_in

counter_time    counter_ip  counter_domain_id
    111111  222222  1
    111111  222224  2
    111111  222225  3
    111111  222232  2
    111111  222221  3
    111111  222223  4
    1111311     22223422    5



 3 Table Out Counter
counter_time    counter_ip  counter_domain_id
    111111  222222  1
    111111  222222  2
    111111  222222  3
    111111  222226  2
    111111  222221  3
    111111  222222  4
1309351485  2130708581  5
    1309351485  2130708581  4
    1309710116  2130706433  4
    1309351485  2130708581  1
1309710274  2130706433  1

Ok what i want todo is join the tables 1 , 2 , 3 and group them together, and count them.

trader_id|DOMAIN|IN|OUT
--------------------------
1|google.com|3|1
2|yahoo.com|1|2

I tried many things, but i get always wrong results. How do i join right the queries that i get results i want

*EDIT

The query attempted:

SELECT traders.trader_id, traders.domain, COUNT( counter_in.counter_domain_id ) AS today_in, COUNT( counter_out.counter_domain_id ) AS today_out
FROM traders
JOIN counter_in ON traders.trader_id = counter_in.counter_domain_id
JOIN counter_out ON traders.trader_id = counter_out.counter_domain_id
GROUP BY traders.trader_id, traders.domain
LIMIT 0 , 30

Results:

trader_id   domain  today_in    today_out
1   bing.com    3   3
2   google.com  4   4
3   yahoo.com   4   4
4   msn.com     3   3
5   yandex.com  1   1

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

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

发布评论

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

评论(2

提笔书几行 2024-12-03 06:34:51
SELECT
  TableTraders.trader_id,
  TableTraders.Domain,
  COUNT(TableIn.trader_id) AS in_count,
  COUNT(TableOut.trader_id) AS out_count
FROM TableTraders 
  JOIN TableIn ON TableTraders.trader_id = TableIn.trader_id
  JOIN TableOut ON TableTraders.trader_id = TableOut.trader_id
GROUP BY TableTraders.trader_id, TableTraders.Domain

不确定上面示例中的表名称是什么,因为它们包含空格。替换正确的表名。

SELECT
  TableTraders.trader_id,
  TableTraders.Domain,
  COUNT(TableIn.trader_id) AS in_count,
  COUNT(TableOut.trader_id) AS out_count
FROM TableTraders 
  JOIN TableIn ON TableTraders.trader_id = TableIn.trader_id
  JOIN TableOut ON TableTraders.trader_id = TableOut.trader_id
GROUP BY TableTraders.trader_id, TableTraders.Domain

Not certain what your table names are from your example above, since they contain spaces. Substitute the correct table names.

挽心 2024-12-03 06:34:51

看来另一个答案可能会给你某种笛卡尔结果,从而重复结果。让您的交易者加入到各自输入/输出表中的不同子查询计数。

SELECT
      TT.trader_id,
      TT.Domain,
      TIN.InCount,
      TOUT.OutCount
   FROM 
      TableTraders TT

         LEFT Join ( select Trader_ID, count(*) as InCount
                   from TableIn
                   group by Trader_ID ) TIN
            on TT.Trader_ID = TIN.Trader_ID

         LEFT Join ( select Trader_ID, count(*) as InCount
                   from TableOut
                   group by Trader_ID ) TOUT
            on TT.Trader_ID = TOUT.Trader_ID

如果您希望每个输入/输出都有不同的 IP 地址,只需更改

COUNT(*) 
to
COUNT( Distinct IP )

It appears the other answer may be giving you some sort of Cartesian result thus duplicating results. Take your traders and join to distinct sub-query counts from respective in / out tables.

SELECT
      TT.trader_id,
      TT.Domain,
      TIN.InCount,
      TOUT.OutCount
   FROM 
      TableTraders TT

         LEFT Join ( select Trader_ID, count(*) as InCount
                   from TableIn
                   group by Trader_ID ) TIN
            on TT.Trader_ID = TIN.Trader_ID

         LEFT Join ( select Trader_ID, count(*) as InCount
                   from TableOut
                   group by Trader_ID ) TOUT
            on TT.Trader_ID = TOUT.Trader_ID

If you want it by DISTINCT IP address per in/out, just change

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