質問編集履歴
6
指摘事項の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,35 +6,87 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
|
9
|
+
```sql
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
-
|
13
|
+
# テーブル作成
|
14
14
|
|
15
|
-
|
15
|
+
CREATE TABLE `trump` (
|
16
16
|
|
17
|
-
|
17
|
+
`id` int(11) NOT NULL,
|
18
18
|
|
19
|
-
|
19
|
+
`card_type` varchar(40) NOT NULL COMMENT 'カード種別',
|
20
20
|
|
21
|
-
|
21
|
+
`created_at` datetime DEFAULT NULL COMMENT '作成日時'
|
22
22
|
|
23
|
-
|
23
|
+
);
|
24
24
|
|
25
|
-
|1|Q|2020-10-01 14:00:00|
|
26
25
|
|
27
|
-
|2|K|2020-10-01 11:00:00|
|
28
26
|
|
29
|
-
|
27
|
+
# インサート
|
30
28
|
|
31
|
-
|
29
|
+
INSERT INTO `trump` (`id`, `card_type`, `created_at`) VALUES
|
32
30
|
|
33
|
-
|
31
|
+
(1, "K", "2020-10-01 11:00:00"),
|
34
32
|
|
35
|
-
|
33
|
+
(1, "K", "2020-10-01 12:00:00"),
|
36
34
|
|
35
|
+
(1, "K", "2020-10-01 13:00:00"),
|
36
|
+
|
37
|
+
(1, "Q", "2020-10-01 13:00:00"),
|
38
|
+
|
39
|
+
(1, "Q", "2020-10-01 14:00:00"),
|
40
|
+
|
41
|
+
(2, "K", "2020-10-01 11:00:00"),
|
42
|
+
|
43
|
+
(2, "K", "2020-10-01 12:00:00"),
|
44
|
+
|
45
|
+
(2, "Q", "2020-10-01 11:00:00"),
|
46
|
+
|
47
|
+
(2, "Q", "2020-10-01 13:00:00"),
|
48
|
+
|
49
|
+
(3, "JOKER", "2020-10-01 11:00:00"),
|
50
|
+
|
51
|
+
(3, "JOKER", "2020-10-01 12:00:00");
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
# データ取得
|
56
|
+
|
57
|
+
select * from trump;
|
58
|
+
|
59
|
+
+----+-----------+---------------------+
|
60
|
+
|
61
|
+
| id | card_type | created_at |
|
62
|
+
|
63
|
+
+----+-----------+---------------------+
|
64
|
+
|
65
|
+
| 1 | K | 2020-10-01 11:00:00 |
|
66
|
+
|
67
|
+
| 1 | K | 2020-10-01 12:00:00 |
|
68
|
+
|
69
|
+
| 1 | K | 2020-10-01 13:00:00 |
|
70
|
+
|
71
|
+
| 1 | Q | 2020-10-01 13:00:00 |
|
72
|
+
|
73
|
+
| 1 | Q | 2020-10-01 14:00:00 |
|
74
|
+
|
75
|
+
| 2 | K | 2020-10-01 11:00:00 |
|
76
|
+
|
77
|
+
| 2 | K | 2020-10-01 12:00:00 |
|
78
|
+
|
79
|
+
| 2 | Q | 2020-10-01 11:00:00 |
|
80
|
+
|
81
|
+
| 2 | Q | 2020-10-01 13:00:00 |
|
82
|
+
|
83
|
+
| 3 | JOKER | 2020-10-01 11:00:00 |
|
84
|
+
|
37
|
-
|3|
|
85
|
+
| 3 | JOKER | 2020-10-01 12:00:00 |
|
86
|
+
|
87
|
+
+----+-----------+---------------------+
|
88
|
+
|
89
|
+
```
|
38
90
|
|
39
91
|
|
40
92
|
|
@@ -117,53 +169,3 @@
|
|
117
169
|
|
118
170
|
|
119
171
|
どのようなクエリを書けば良いのか、ご教示いただきたいです。よろしくお願いします。
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
サンプルのテーブル・レコードのクエリを記載します。
|
126
|
-
|
127
|
-
```sql
|
128
|
-
|
129
|
-
# テーブル作成
|
130
|
-
|
131
|
-
CREATE TABLE `trump` (
|
132
|
-
|
133
|
-
`id` int(11) NOT NULL,
|
134
|
-
|
135
|
-
`card_type` varchar(40) NOT NULL COMMENT 'カード種別',
|
136
|
-
|
137
|
-
`created_at` datetime DEFAULT NULL COMMENT '作成日時'
|
138
|
-
|
139
|
-
);
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
# インサート
|
144
|
-
|
145
|
-
INSERT INTO `trump` (`id`, `card_type`, `created_at`) VALUES
|
146
|
-
|
147
|
-
(1, "K", "2020-10-01 11:00:00"),
|
148
|
-
|
149
|
-
(1, "K", "2020-10-01 12:00:00"),
|
150
|
-
|
151
|
-
(1, "K", "2020-10-01 13:00:00"),
|
152
|
-
|
153
|
-
(1, "Q", "2020-10-01 13:00:00"),
|
154
|
-
|
155
|
-
(1, "Q", "2020-10-01 14:00:00"),
|
156
|
-
|
157
|
-
(2, "K", "2020-10-01 11:00:00"),
|
158
|
-
|
159
|
-
(2, "K", "2020-10-01 12:00:00"),
|
160
|
-
|
161
|
-
(2, "Q", "2020-10-01 11:00:00"),
|
162
|
-
|
163
|
-
(2, "Q", "2020-10-01 13:00:00"),
|
164
|
-
|
165
|
-
(3, "JOKER", "2020-10-01 11:00:00"),
|
166
|
-
|
167
|
-
(3, "JOKER", "2020-10-01 12:00:00");
|
168
|
-
|
169
|
-
```
|
5
サンプルsql追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -117,3 +117,53 @@
|
|
117
117
|
|
118
118
|
|
119
119
|
どのようなクエリを書けば良いのか、ご教示いただきたいです。よろしくお願いします。
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
サンプルのテーブル・レコードのクエリを記載します。
|
126
|
+
|
127
|
+
```sql
|
128
|
+
|
129
|
+
# テーブル作成
|
130
|
+
|
131
|
+
CREATE TABLE `trump` (
|
132
|
+
|
133
|
+
`id` int(11) NOT NULL,
|
134
|
+
|
135
|
+
`card_type` varchar(40) NOT NULL COMMENT 'カード種別',
|
136
|
+
|
137
|
+
`created_at` datetime DEFAULT NULL COMMENT '作成日時'
|
138
|
+
|
139
|
+
);
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
# インサート
|
144
|
+
|
145
|
+
INSERT INTO `trump` (`id`, `card_type`, `created_at`) VALUES
|
146
|
+
|
147
|
+
(1, "K", "2020-10-01 11:00:00"),
|
148
|
+
|
149
|
+
(1, "K", "2020-10-01 12:00:00"),
|
150
|
+
|
151
|
+
(1, "K", "2020-10-01 13:00:00"),
|
152
|
+
|
153
|
+
(1, "Q", "2020-10-01 13:00:00"),
|
154
|
+
|
155
|
+
(1, "Q", "2020-10-01 14:00:00"),
|
156
|
+
|
157
|
+
(2, "K", "2020-10-01 11:00:00"),
|
158
|
+
|
159
|
+
(2, "K", "2020-10-01 12:00:00"),
|
160
|
+
|
161
|
+
(2, "Q", "2020-10-01 11:00:00"),
|
162
|
+
|
163
|
+
(2, "Q", "2020-10-01 13:00:00"),
|
164
|
+
|
165
|
+
(3, "JOKER", "2020-10-01 11:00:00"),
|
166
|
+
|
167
|
+
(3, "JOKER", "2020-10-01 12:00:00");
|
168
|
+
|
169
|
+
```
|
4
バージョン追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
mysqlバージョン: `v5.7`
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
### やりたいこと
|
2
6
|
|
3
7
|
|
3
書式改善・条件追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -36,7 +36,7 @@
|
|
36
36
|
|
37
37
|
上記のようなテーブルがあるとします。
|
38
38
|
|
39
|
-
以下の条件に合ったデータを `id` でグループ化して取得したいと思っています。
|
39
|
+
以下の条件に合ったデータを `id` でグループ化して `mysql` で取得したいと思っています。
|
40
40
|
|
41
41
|
|
42
42
|
|
2
タグ追加
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
1
書式改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,43 +10,27 @@
|
|
10
10
|
|
11
11
|
|:--|:--:|--:|
|
12
12
|
|
13
|
-
|1|
|
13
|
+
|1|K|2020-10-01 11:00:00|
|
14
14
|
|
15
|
-
|1|
|
15
|
+
|1|K|2020-10-01 12:00:00|
|
16
16
|
|
17
|
-
|1|
|
17
|
+
|1|K|2020-10-01 13:00:00|
|
18
18
|
|
19
19
|
|1|Q|2020-10-01 13:00:00|
|
20
20
|
|
21
21
|
|1|Q|2020-10-01 14:00:00|
|
22
22
|
|
23
|
-
|2|
|
23
|
+
|2|K|2020-10-01 11:00:00|
|
24
24
|
|
25
|
-
|2|
|
25
|
+
|2|K|2020-10-01 12:00:00|
|
26
|
+
|
27
|
+
|2|Q|2020-10-01 11:00:00|
|
26
28
|
|
27
29
|
|2|Q|2020-10-01 13:00:00|
|
28
|
-
|
29
|
-
|2|Q|2020-10-01 14:00:00|
|
30
30
|
|
31
31
|
|3|`JOKER`|2020-10-01 11:00:00|
|
32
32
|
|
33
33
|
|3|`JOKER`|2020-10-01 12:00:00|
|
34
|
-
|
35
|
-
|3|K|2020-10-01 11:00:00|
|
36
|
-
|
37
|
-
|3|K|2020-10-01 13:00:00|
|
38
|
-
|
39
|
-
|4|Q|2020-10-01 11:00:00|
|
40
|
-
|
41
|
-
|4|Q|2020-10-01 12:00:00|
|
42
|
-
|
43
|
-
|4|K|2020-10-01 13:00:00|
|
44
|
-
|
45
|
-
|4|K|2020-10-01 14:00:00|
|
46
|
-
|
47
|
-
|5|`JOKER`|2020-10-01 11:00:00|
|
48
|
-
|
49
|
-
|5|`JOKER`|2020-10-01 12:00:00|
|
50
34
|
|
51
35
|
|
52
36
|
|
@@ -90,6 +74,10 @@
|
|
90
74
|
|
91
75
|
|
92
76
|
|
77
|
+
※ 同率一位の場合、 `created_at` で比較し、新しい方が **1位** とする。
|
78
|
+
|
79
|
+
|
80
|
+
|
93
81
|
|
94
82
|
|
95
83
|
###### ■ 結果として欲しいレコード
|
@@ -104,15 +92,13 @@
|
|
104
92
|
|
105
93
|
|:--:|:--:|:--:|:--:|:--:|
|
106
94
|
|
107
|
-
|1|
|
95
|
+
|1|K|2020-10-01 **13:00:00**|Q|2020-10-01 **14:00:00**|
|
108
96
|
|
109
|
-
|2|Q|2020-10-01 **1
|
97
|
+
|2|Q|2020-10-01 **13:00:00** |K|2020-10-01 **12:00:00**|
|
110
98
|
|
111
|
-
|3|
|
99
|
+
|3| `JOKER` | 2020-10-01 **12:00:00**|**null**|**null** |
|
112
100
|
|
113
|
-
|4|K|2020-10-01 **14:00:00**|Q|2020-10-01 **12:00:00**|
|
114
101
|
|
115
|
-
|5| `JOKER` | 2020-10-01 **12:00:00**|**null**|**null** |
|
116
102
|
|
117
103
|
|
118
104
|
|