P
P
Push Pull2014-06-18 04:01:44
MySQL
Push Pull, 2014-06-18 04:01:44

MySQL. How to make grouping for each condition in where?

The actual request:

SELECT st.id, st.name, st.date_time, bs.date_time 
  FROM stat as st 
  LEFT JOIN bonus as bs ON st.date_time = bs.date_time 
  WHERE st.name IN('lol', 'strange') 
  GROUP BY MONTH(st.date_time)

stat:
id - int, name - varchar(), date_time = datetime
bonus:
id - int, data = varchar, date_time = datetime
As a result - the grouping by month goes on, but st.name is one or the other, but it would be necessary to for each there was a grouping by month.
SELECT for everyone will not work, fields - conditions more than 1 name
I.e. so that the output is:
id name Month
1 lol April
2 strange April
1 lol May
2 strange May

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis Morozov, 2014-06-18
@deadbyelpy

SELECT st.id AS id, st.name AS name, MONTH(st.date_time) AS Month 
  FROM stat as st 
  LEFT JOIN bonus as bs ON st.date_time = bs.date_time 
  WHERE st.name IN('lol', 'strange') 
  GROUP BY st.id, st.name, MONTH(st.date_time)

A
AxisPod, 2014-06-18
@AxisPod

Aren't you afraid that the database will say: "Well, you have queries" and freeze? Database optimizers in hell are shown execution plans for such queries.

K
kuznetsovin, 2014-06-18
@kuznetsovin

SELECT DISTINCT st.id, st.name, MONTH(st.date_time) 
  FROM stat as st 
  LEFT JOIN bonus as bs ON st.date_time = bs.date_time 
  WHERE st.name IN('lol', 'strange') 
ORDER BY st.id, st.name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question