V
V
Vladislav2018-04-15 18:13:43
MySQL
Vladislav, 2018-04-15 18:13:43

How to pull data from two tables grouped by date?

Hello! Need help making a request.
Table structure:
transitions
id
datetime
affiliates
id
transition_id
order_sum
affiliate_sum
It is necessary to extract
COUNT(transitions.id), COUNT(affiliates.id), SUM(affiliates.affiliate_sum), SUM(affiliates.order_sum)
grouped by days (transitions.datetime)
affiliates .transition_id =
transitions.id as a result, a table of the form is required:
DATE 1 | Number of transitions.id | Number of affiliates.id | Sum affiliates.order_sum | affiliates.affiliate_sum
DATE 2 | Number of transitions.id | Number of affiliates.id | Sum affiliates.order_sum | Amount affiliates.affiliate_sum
DATE 3 | Number of transitions.id | Number of affiliates.id | Sum affiliates.order_sum | Amount affiliates.affiliate_sum
and so on

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bioGavs, 2018-04-16
@bioGavs

probably so

SELECT DATE_FORMAT(t.datetime, '%Y-%m-%d') AS `date`, count(t.id), count(a.id), sum(a.affiliate_sum), sum(a.order_sum)  
FROM transitions AS t
INNER JOIN affiliates a ON a.transition_id = t.id
GROUP BY `date`

A
Alexander, 2018-04-17
@Tuborg

SELECT <something>
FROM <table> AS table1
LEFT JOIN <table> AS table2 ON <conditions>
WHERE <conditions>
GROUP BY date

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question