Mysql分组与计数
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定上面示例中的表名称是什么,因为它们包含空格。替换正确的表名。
Not certain what your table names are from your example above, since they contain spaces. Substitute the correct table names.
看来另一个答案可能会给你某种笛卡尔结果,从而重复结果。让您的交易者加入到各自输入/输出表中的不同子查询计数。
如果您希望每个输入/输出都有不同的 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.
If you want it by DISTINCT IP address per in/out, just change