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