MySQL group by with left join

£可爱£侵袭症+ 提交于 2019-12-11 06:37:28

问题


I am trying to do a very complex query (at least extremely complex for me not for YOU :) )

I have users and comments table.

SQL Fiddle: http://sqlfiddle.com/#!9/b1f845/2

select user_id, status_id from comments where user_id in (2,3);
+---------+-----------+
| user_id | status_id |
+---------+-----------+
|       2 |        10 |
|       2 |        10 |
|       2 |        10 |
|       2 |         7 |
|       2 |         7 |
|       2 |        10 |
|       3 |         9 |
|       2 |         9 |
|       2 |         6 |
+---------+-----------+

If I use

select user_id, status_id from comments where user_id in (2,3)

It returns a lot of duplicate values.

What I want to get if possible.

If you see status_id = 10 has user_id= 2,3 and 4 and 2 multiple times. So from here I want to get maximum of latest user_id (unique) so for example,

it will be user_id = 4 and 2 now the main complex part. I now want to get users information of user_id= 4 and 2 in one column so that at the end I can get something like this

status_id |  userOneUserName | userTwoUserName
 10            sadek4             iamsadek2
 ---------------------------------------------
 7        |    iamsadek2     |      null
 ---------------------------------------------
 9 .      |    iamsadek2     |      sadek2
 ---------------------------------------------
 6        |    iamsadek2     |       null

How can I achieve such a complex things. Currently I have to do it using application logic.

Thank you for your time.


回答1:


I think this might be what you literally want here:

SELECT DISTINCT
    status_id,
    (SELECT MAX(user_id) FROM comments c2 WHERE c1.status_id = c2.status_id) user_1,
    (SELECT user_id FROM comments c2 WHERE c1.status_id = c2.status_id
     ORDER BY user_id LIMIT 1 OFFSET 1) user_2
FROM comments c1
WHERE user_id IN (2,3);

Demo (your update Fiddle)

We can use correlated subqueries to find the max user_id and second-to-max user_id for each status_id, and then spin each of those out as two separate columns. Using a GROUP_CONCAT approach might be preferable here, since it would also allow you to easily accommodate any numbers of users as a CSV list.

Also, if you were using MySQL 8+ or greater, then we could take advantage of the rank analytic functions, which would also be easier.




回答2:


select status_id, GROUP_CONCAT(distinct(user_id) SEPARATOR ',')
from comments
group by status_id



回答3:


I would suggest using GROUP BY and GROUP_CONCAT, e.g. like so:

SELECT status_id, GROUP_CONCAT(userName) AS users, GROUP_CONCAT(DISTINCT c.user_id) AS user_ids 
FROM (
  SELECT DISTINCT status_id, user_id FROM comments WHERE user_id in (2,3)
) c
JOIN users u ON (c.user_id = u.id)
GROUP BY status_id
ORDER BY status_id DESC


来源:https://stackoverflow.com/questions/51749862/mysql-group-by-with-left-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!