MYSQL select only users who made more than 1 record, without grouping -
i have userlogs table , query logs monitoring
select from_unixtime(logdate/1000) logdate, userid, useraction userlogs logdate > subdate(now(), interval 1 day )
i want improve showing in result users have more 1 record, without grouping.
example:
user a made 1 record day, user b made 5 records , user c made 3 records same day. sql result show 8 columns - 5 records user b , 3 records user c.
<table border='1'> <tr> <td>userid</td> <td>time</td> <td>action</td> </tr> <tr> <td>b</td> <td>11.56</td> <td>post</td> </tr> <tr> <td>b</td> <td>11.57</td> <td>delete</td> </tr> <tr> <td>b</td> <td>11.58</td> <td>comment</td> </tr> <tr> <td>b</td> <td>11.59</td> <td>delete</td> </tr> <tr> <td>b</td> <td>11.60</td> <td>edit</td> </tr> <tr> <td>c</td> <td>11.60</td> <td>edit</td> </tr> <tr> <td>c</td> <td>11.60</td> <td>comment</td> </tr> <tr> <td>c</td> <td>11.60</td> <td>post</td> </tr> </table>
select from_unixtime(logdate/1000) logdate, userid userlogs ul1 inner join (select userid, from_unixtime(logdate/1000) logdate, count(1) cnt userlogs group userid, from_unixtime(logdate/1000) having count(1)>1) ul2 on ul1.userid = ul2.userid , ul1.logdate = from_unixtime(ul1.logdate/1000) logdate > subdate(now(), interval 1 day )
what generate subset of users days users having more 1 entry. joins base set return 8 records users on days; excluding users having count of 1.
instead of join use exists statement may faster.
Comments
Post a Comment