Select distinct ordered pairs from table join grouped by event's most recent date

ぐ巨炮叔叔 提交于 2019-12-13 10:17:33

问题


Following the post Select distinct ordered pair from table join

How can I select the most recent John's joined rows grouped by ordered pair, regardless the order (e.g. John -> Jane or Jane -> John)?

First table:

table_a
+-----------+--------------+-------------------+
|    id     |     name     |     created_at    |
+-----------+--------------+-------------------+
|     1     |     John     |2016-08-26 15:40:21|
+-----------+--------------+-------------------+
|     2     |     Jane     |2016-08-26 15:37:21|
+-----------+--------------+ ------------------+
|     3     |     Jane     |2016-08-26 15:38:21|
+-----------+--------------+-------------------+
|     4     |     Tara     |2016-08-26 15:39:21|
+-----------+--------------+-------------------+   

Second Table:

table_b
+-----------+-------------------+-------------+-------------+
|    id     |     id_table_a    |    name     |   message   |
+-----------+-------------------+-------------+-------------+
|    1      |          1        |    Jane     |   Test 1    |
+-----------+-------------------+-------------+-------------+
|    2      |          2        |    John     |   Test 2    |
+-----------+-------------------+-------------+-------------+
|    3      |          3        |    Sammy    |   Test 3    |
+-----------+-------------------+-------------+-------------+
|    4      |          4        |    John     |   Test 4    |
+-----------+-------------------+-------------+-------------+

One possible result

+-----------+-------------+-------------+-------------+-------------------+
|    id     |   name_a    |   name_b    |   message   |     created_at    |
+-----------+-------------+-------------+-------------+-------------------+
|    1      |   John      |   Jane      |   Test 1    |2016-08-26 15:40:21|
+-----------+-------------+-------------+-------------+-------------------+
|    4      |   Tara      |   John      |   Test 4    |2016-08-26 15:39:21|
+-----------+-------------+-------------+-------------+-------------------+

sqlfiddle

Thanks a lot!


回答1:


Try this:

(select a.id, a.name, b.name, b.message, a.created_at from a left join b on a.id=b.aid where a.name='John' order by a.created_at desc limit 1)
union all
(select a.id, a.name, b.name, b.message, a.created_at from a left join b on a.id=b.aid where b.name='John' order by a.created_at desc limit 1)



回答2:


This gets a bit messy. Here's an option using user-defined variables to establish a row_number within each group of results and then filtering by row number = 1:

select *
from (
  select *,
    @rn := if(@prev_name_a = name_a and @prev_name_b = name_b, @rn+1,
                if(@prev_name_a:=name_a,1,
                   if(@prev_name_b:=name_b, 1, 1)
                )
             ) rn
  from (
    select least(a.name, b.name) as name_a, 
        greatest(a.name, b.name) as name_b, 
        a.created_at,
        a.id, 
        b.message
    from table_a a 
      join table_b b on a.id = b.id_table_a
  ) t cross join (select @rn:=0, @prev_name_a:=null, @prev_name_b:=null) t1
  order by name_a, name_b, created_at desc
  ) t
where 'John' in (name_a, name_b) and rn = 1
  • SQL Fiddle Demo


来源:https://stackoverflow.com/questions/39171187/select-distinct-ordered-pairs-from-table-join-grouped-by-events-most-recent-dat

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