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

回答編集履歴

2

修正

2019/01/07 03:11

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -7,14 +7,14 @@
7
7
  ・相関副問合せ
8
8
  ```SQL
9
9
  select id as user_id, name
10
- , (select min(login_date) from ログイン where user_id=usr.id and login_date>='2018/11/31 00:00:00') as login_date
10
+ , (select min(login_date) from ログイン where user_id=usr.id and login_date>='2018/11/30 00:00:00') as login_date
11
11
  from ユーザー as usr
12
12
  ```
13
13
  ・キーを揃えて結合
14
14
  ```SQL
15
15
  select id as user_id, name, login_date
16
16
  from ユーザー as usr left join (
17
- select user_id, min(login_date) as login_date from ログイン where login_date>='2018/11/31 00:00:00' group by user_id
17
+ select user_id, min(login_date) as login_date from ログイン where login_date>='2018/11/30 00:00:00' group by user_id
18
18
  ) login
19
19
  on user.id=login.user_id
20
20
  ```

1

修正

2019/01/07 03:11

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -1,17 +1,20 @@
1
1
  DBMSやデータ状況によってSQLの効率は変わります。
2
2
 
3
3
  主に以下のような問合せによって取得できます。
4
+ > 引用テキストログイン日時が2018/11/31 00:00:00以降のものを条件とし、ログイン日時が一番古いものを取得する
5
+
6
+ ※上記条件を見落としていたので修正
4
7
  ・相関副問合せ
5
8
  ```SQL
6
9
  select id as user_id, name
7
- , (select max(login_date) from ログイン where user_id=usr.id) as login_date
10
+ , (select min(login_date) from ログイン where user_id=usr.id and login_date>='2018/11/31 00:00:00') as login_date
8
11
  from ユーザー as usr
9
12
  ```
10
13
  ・キーを揃えて結合
11
14
  ```SQL
12
15
  select id as user_id, name, login_date
13
16
  from ユーザー as usr left join (
14
- select user_id, max(login_date) as login_date from ログイン group by user_id
17
+ select user_id, min(login_date) as login_date from ログイン where login_date>='2018/11/31 00:00:00' group by user_id
15
18
  ) login
16
19
  on user.id=login.user_id
17
20
  ```