回答編集履歴
3
けっか
test
CHANGED
@@ -145,3 +145,17 @@
|
|
145
145
|
) as sub2 using(post_id)
|
146
146
|
|
147
147
|
```
|
148
|
+
|
149
|
+
- 結果
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
|post_id|名前|件名1|参加1|件名2|参加2|
|
154
|
+
|
155
|
+
|--:|:--|:--|--:|:--|--:|
|
156
|
+
|
157
|
+
|1|山田太郎|参加|1|不参加|0|
|
158
|
+
|
159
|
+
|2|田中花子|参加|1|参加|1|
|
160
|
+
|
161
|
+
|NULL|NULL|NULL|2|NULL|1|
|
2
むりやり
test
CHANGED
@@ -63,3 +63,85 @@
|
|
63
63
|
(3,'合同イベント','鈴木一郎','不参加','不参加');
|
64
64
|
|
65
65
|
```
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
# 無理やりやってみる
|
70
|
+
|
71
|
+
```SQL
|
72
|
+
|
73
|
+
create table at_postmeta(post_id int,meta_key varchar(10),meta_value varchar(10));
|
74
|
+
|
75
|
+
insert into at_postmeta values
|
76
|
+
|
77
|
+
(1,'イベント名','役員会議'),
|
78
|
+
|
79
|
+
(1,'名前','山田太郎'),
|
80
|
+
|
81
|
+
(1,'件名1','参加'),
|
82
|
+
|
83
|
+
(1,'件名2','不参加'),
|
84
|
+
|
85
|
+
(2,'イベント名','役員会議'),
|
86
|
+
|
87
|
+
(2,'名前','田中花子'),
|
88
|
+
|
89
|
+
(2,'件名1','参加'),
|
90
|
+
|
91
|
+
(2,'件名2','参加'),
|
92
|
+
|
93
|
+
(3,'イベント名','合同イベント'),
|
94
|
+
|
95
|
+
(3,'名前','鈴木一郎'),
|
96
|
+
|
97
|
+
(3,'件名1','不参加'),
|
98
|
+
|
99
|
+
(3,'件名2','不参加');
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
- 検索
|
104
|
+
|
105
|
+
```SQL
|
106
|
+
|
107
|
+
select post_id,名前,件名1,参加1,件名2,参加2
|
108
|
+
|
109
|
+
from (
|
110
|
+
|
111
|
+
select t1.post_id,
|
112
|
+
|
113
|
+
group_concat(case meta_key when "名前" then meta_value else null end) 名前,
|
114
|
+
|
115
|
+
group_concat(case meta_key when "件名1" then meta_value else null end) 件名1,
|
116
|
+
|
117
|
+
group_concat(case meta_key when "件名2" then meta_value else null end) 件名2
|
118
|
+
|
119
|
+
from at_postmeta as t1
|
120
|
+
|
121
|
+
inner join (select post_id from at_postmeta where meta_key='イベント名' and meta_value='役員会議') as t2
|
122
|
+
|
123
|
+
on t1.post_id=t2.post_id
|
124
|
+
|
125
|
+
group by post_id
|
126
|
+
|
127
|
+
) as sub1 right join (
|
128
|
+
|
129
|
+
select t1.post_id,
|
130
|
+
|
131
|
+
sum((meta_key="件名1")*(meta_value="参加")) 参加1,
|
132
|
+
|
133
|
+
sum((meta_key="件名2")*(meta_value="参加")) 参加2
|
134
|
+
|
135
|
+
from at_postmeta as t1
|
136
|
+
|
137
|
+
inner join (select post_id from at_postmeta where meta_key='イベント名' and meta_value='役員会議') as t2
|
138
|
+
|
139
|
+
on t1.post_id=t2.post_id
|
140
|
+
|
141
|
+
group by post_id
|
142
|
+
|
143
|
+
with rollup
|
144
|
+
|
145
|
+
) as sub2 using(post_id)
|
146
|
+
|
147
|
+
```
|
1
調整
test
CHANGED
@@ -37,3 +37,29 @@
|
|
37
37
|
```
|
38
38
|
|
39
39
|
だと思います
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
# テーブルの設計から
|
44
|
+
|
45
|
+
```SQL
|
46
|
+
|
47
|
+
create table tbl(id int primary key,
|
48
|
+
|
49
|
+
イベント名 varchar(10),
|
50
|
+
|
51
|
+
名前 varchar(10),
|
52
|
+
|
53
|
+
件名1 varchar(10),
|
54
|
+
|
55
|
+
件名2 varchar(10));
|
56
|
+
|
57
|
+
insert into tbl values
|
58
|
+
|
59
|
+
(1,'役員会議','山田太郎','参加','不参加'),
|
60
|
+
|
61
|
+
(2,'役員会議','田中花子','参加','参加'),
|
62
|
+
|
63
|
+
(3,'合同イベント','鈴木一郎','不参加','不参加');
|
64
|
+
|
65
|
+
```
|