Monday 15 September 2014

group by - MySQL max selecting max value but pairing with incorrect row -


I'll show you how my database looks with some example data:

  | ID | Type | ID_User | Date | | 0 | 0 | 1 | 1 | | 1 | 0 | 2 | 2 | | 2 | 1 | 1 | 3 | | 3 | 0 | 3 | 4 | | 4 | 0 | 1 | 5 | | 5 | 1 | 1 | 6 |   

What is this tracking when people go to people or join the group and how many people I am trying to get in a group. For example, for example, the history of this table will be as follows: (Type 0 = Adds, Type 1 = Bandages)

  User gets at 1 time 1 user gets 2 times 2 users leave at 1 time 3 Users meet at 3 times 4 Users 1 time 5 Users 1 connects with time 6   

So if I say where in the group How many people are, then this will be 2 because the user is 2 times the number Gives away and only the user is 2, now in 3 groups.

I am playing for the past hours and half, but I feel that nothing can be found to work. I'm really confused with Max () because he is not returning what I think would be the right result (although I know I'm completely doing something wrong)

  SELECT *, MAX (date) myTable GROUP to id_user   

The above is my current thinking, but it is not giving me anything that I want.

I went to PHPMyAdmin and tried the very greatest date (I know that you can do this very simple MAX, but not what I am looking for. Test): <

  SELECT *, myTable from MAX (date)   

This, however, will return the first line to the list. So I played around a bit more and put the maximum (date) as D and what I found was actually returning the first column and then slapping on the highest date:

  | ID | Type | ID_User | Date | D | | 0 | 0 | 1 | 1 | 6 |   

I have tried to googling it and looked through other SO queries but nothing can solve my problem if anyone has any idea which I am really wrong with the max or have any suggestions on this approach, so I am truly grateful. (I realize that my query must be more intensive.) Anyways, thanks.

The easiest way to get people into the group is:

 myTable (when type = 0 then 1 when type = 1 then -1 end)   

Getting people's list is a bit more challenging You want people Those who enter but have not left. Perhaps, a person can enter and leave many times, to solve it, let's go to each person. Count me and use logic to that:

  sum by the id_user from myTable group (type = 0) & gt; Amount (type = 1)    

No comments:

Post a Comment