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

回答編集履歴

1

追加

2019/01/11 01:00

投稿

yambejp
yambejp

スコア117902

answer CHANGED
@@ -25,4 +25,32 @@
25
25
  and dat>t1.dat
26
26
  )
27
27
  and not_a is not null
28
- ```
28
+ ```
29
+
30
+ # 追加
31
+ ```SQL
32
+ create table user(name varchar(10),age int, sex set('男','女'));
33
+ insert into user values
34
+ ('Bさん',20,'男'),
35
+ ('Cさん',30,'女');
36
+ ```
37
+
38
+ - ユーザー情報つき
39
+ ```SQL
40
+ select * from (
41
+ select no,dat,elt(field('Aさん',to_user,from_user),from_user,to_user) as not_a
42
+ from tbl as t1
43
+ having not exists(select 1
44
+ from tbl
45
+ where 'Aさん' in(to_user,from_user)
46
+ and elt(field('Aさん',to_user,from_user),from_user,to_user)=not_a
47
+ and dat>t1.dat
48
+ )
49
+ and not_a is not null
50
+ )
51
+ as t2
52
+ inner join user as t3 on t2.not_a=t3.name
53
+ ```
54
+ ※上記だとDさんがuserテーブルにないので消えてしまいます
55
+ ユーザー情報のないuserもnullで個人情報を表示したいなら
56
+ inner joinをleft joinに変えてください