Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use where clause after a group by result in mysql

Tags:

mysql

I have the following table,

    mysql> select username,authdate from radpostauth;
+------------+---------------------+
| username   | authdate            |
+------------+---------------------+
| timetest   | 2013-05-21 22:44:46 |
| timetest   | 2013-05-21 23:54:20 |
| coconeja   | 2013-05-22 00:01:42 |
| coconeja   | 2013-05-22 00:06:35 |
| coconeja   | 2013-05-25 22:35:34 |
| timetest   | 2013-05-25 23:04:54 |
| distom11   | 2013-05-25 23:10:47 |
| distom11   | 2013-05-25 23:16:42 |
| test       | 2013-05-25 23:45:16 |
| pepe       | 2013-05-26 00:07:00 |
| doce       | 2013-05-26 00:46:48 |
| 6096753968 | 2013-05-26 01:42:30 |
| 2269664468 | 2013-05-26 01:43:57 |
| 2076877624 | 2013-05-26 02:01:53 |
| 4446830988 | 2013-05-26 02:02:28 |
| 2076877624 | 2013-05-26 02:08:53 |
| 3906187975 | 2013-05-26 22:00:30 |
| 3906187975 | 2013-05-26 22:21:44 |
| kk         | 2013-05-26 22:32:20 |
| kk         | 2013-05-26 22:44:19 |
| 160059817  | 2013-05-27 00:53:56 |
| yibced9    | 2013-05-27 13:32:00 |
| yibced9    | 2013-05-27 13:37:53 |
| yibced9    | 2013-05-27 13:38:01 |
| yibced9    | 2013-05-27 13:38:02 |
| yibced9    | 2013-05-27 13:38:03 |
| yibced9    | 2013-05-27 13:38:39 |
| yibced9    | 2013-05-27 13:38:40 |
| yibced9    | 2013-05-27 13:38:41 |
| yibced9    | 2013-05-27 13:44:40 |

I want to find the 'usernames' who first access was in the last 12 hours.

I know how to get the first time each user have been logged with the following query

    mysql> select username,min(authdate) from radpostauth group by username;
+------------+---------------------+
| username   | min(authdate)       |
+------------+---------------------+
| 160059817  | 2013-05-27 00:53:56 |
| 2076877624 | 2013-05-26 02:01:53 |
| 2269664468 | 2013-05-26 01:43:57 |
| 3906187975 | 2013-05-26 22:00:30 |
| 4446830988 | 2013-05-26 02:02:28 |
| 6096753968 | 2013-05-26 01:42:30 |
| coconeja   | 2013-05-22 00:01:42 |
| distom11   | 2013-05-25 23:10:47 |
| doce       | 2013-05-26 00:46:48 |
| kk         | 2013-05-26 22:32:20 |
| pepe       | 2013-05-26 00:07:00 |
| test       | 2013-05-25 23:45:16 |
| timetest   | 2013-05-21 22:44:46 |
| yibced9    | 2013-05-27 13:32:00 |
+------------+---------------------+

so now I am triying to get those usernames doing this:

select *
from radpostauth
where (
    select username,min(authdate)
    from radpostauth group by username
    ) > timediff( now(),maketime(12,0,0,) );

but obviouly it doesn't work...

like image 768
David Mart Avatar asked May 27 '13 20:05

David Mart


People also ask

Can I use WHERE after GROUP BY?

GROUP BY clause is used with the SELECT statement. In the query, GROUP BY clause is placed after the WHERE clause.

Can we use WHERE clause with GROUP BY in MySQL?

The GROUP BY clause groups a set of rows into a set of summary rows by values of columns or expressions. The GROUP BY clause returns one row for each group. In other words, it reduces the number of rows in the result set. In this syntax, you place the GROUP BY clause after the FROM and WHERE clauses.

Can we pass WHERE condition after GROUP BY clause?

Absolutely. It will result in filtering the records on your date range and then grouping it by each day where there is data.

Can we use extract in WHERE clause?

With the help of following MySQL query, we can apply EXTRACT() function with WHERE clause on the dates stored in a table.


1 Answers

select username, min(authdate) 
from radpostauth 
group by username
having min(authdate) > NOW() - INTERVAL 12 HOUR

In SQL, WHERE restricts rows, but HAVING restricts groups.

like image 167
Bill Karwin Avatar answered Sep 26 '22 11:09

Bill Karwin