teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

変更

2020/02/01 12:19

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -7,20 +7,22 @@
7
7
  , age.sub_val as age, address.sub_val as address
8
8
  , thread_follower.follow_date, thread_follower.favorite_date
9
9
  from (
10
+ select follow.actor_id
11
+ , max(
10
- select 'main' as action_name, actor_id as user_id, Null as follow_date, Null as favorite_date
12
+ case when action.action_name='follow' then action.action_date end
13
+ ) as follow_date
14
+ , max(
15
+ case when action.action_name='favorite' then action.action_date end
16
+ ) as favorite_date
11
- from action_datas
17
+ from action_datas follow
12
- where target_type ='user' and actor_id=1 and action_name='follow'
13
- union all
14
- select action_name, target_id, action_date, Null
15
- from action_datas
18
+ left join action_datas action
19
+ on follow.actor_id=action.target_id
20
+ and action.actor_id=1 and action.target_type='user'
16
- where target_type ='user' and actor_id=1 and action_name='follow'
21
+ where follow.target_type='thread' and follow.target_id=20
17
- union all
18
- select action_name, target_id, Null, action_date
19
- from action_datas
22
+ group by follow.actor_id
20
- where target_type ='user' and actor_id=1 and action_name='favorite'
21
23
  ) thread_follower
22
24
  left join main_datas main
23
- on thread_follower.user_id=main.main_id
25
+ on thread_follower.actor_id=main.main_id
24
26
  left join sub_datas age
25
27
  on main.main_id=age.user_id and age.sub_key='age'
26
28
  left join sub_datas address