O
O
Outoverlay2015-12-01 11:39:37
PHP
Outoverlay, 2015-12-01 11:39:37

How to create such query to mysql?

SELECT `id_post`, `time` FROM `posts` GROUP BY `id_post` WHERE `time` BETWEEN FROM_UNIXTIME($min) AND FROM_UNIXTIME($max_time); The problem is GROUP BY `id_post`, and I perfectly understand why, but I don’t have enough experience to form it correctly, how can the resulting value SELECT `id_post`, `time` FROM `posts` GROUP BY `id_post`, be used for WHERE ?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Evgeniy Zavyalov, 2015-12-01
@Outoverlay

GROUP BY is written after WHERE

select tab.*
  from ( SELECT `id_post`, `time` 
               FROM `posts` 
              GROUP BY `id_post` ,  `time` 
           ) tab
 where tab. `time` BETWEEN FROM_UNIXTIME($min) AND FROM_UNIXTIME($max_time);

D
Dmitry Kovalsky, 2015-12-01
@dmitryKovalskiy

1) Given that 'time' is not grouped - in some DBMS such an entry will return an error. What time to display if id_post will be the same 15 and all with different times?
2) WHERE first, then GROUP BY

M
mihelsonkk, 2015-12-01
@mihelsonkk

https://ru.wikipedia.org/wiki/Having_%28SQL%29
if I understand you correctly, you don't need where here.
Can you formulate in plain language what you want to receive as a result of the request?

R
Rsa97, 2015-12-01
@Rsa97

If there are several posts with the same `id_post`, then at `time` the grouping will take the value from one of the posts, it is impossible to tell from which one.
Well, after GROUP BY, you can use HAVING, but not WHERE.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question