回答編集履歴

1

chousei

2019/02/12 03:50

投稿

yambejp
yambejp

スコア114843

test CHANGED
@@ -7,3 +7,125 @@
7
7
  回答した時点で日時を入れれば、answer_atが過去日で
8
8
 
9
9
  correctが0のモノが間違えた質問になります
10
+
11
+
12
+
13
+ # 訂正
14
+
15
+
16
+
17
+ なんか効率悪そうなので考え方を換えます
18
+
19
+
20
+
21
+ ```SQL
22
+
23
+ create table question(qid int primary key,qtext varchar(100)
24
+
25
+ ,a1 varchar(100)
26
+
27
+ ,a2 varchar(100)
28
+
29
+ ,a3 varchar(100)
30
+
31
+ ,a4 varchar(100)
32
+
33
+ ,correct_a int);
34
+
35
+ insert into question values
36
+
37
+ ( 1,"q01_text","q01_a1","q01_a2","q01_a3","q01_a4",1),
38
+
39
+ ( 2,"q02_text","q0_a1","q02_a2","q02_a3","q02_a4",2),
40
+
41
+ ( 3,"q03_text","q03_a1","q03_a2","q03_a3","q03_a4",3),
42
+
43
+ ( 4,"q04_text","q04_a1","q04_a2","q4_a3","q4_a4",3),
44
+
45
+ ( 5,"q05_text","q05_a1","q05_a2","q5_a3","q5_a4",4),
46
+
47
+ ( 6,"q06_text","q06_a1","q06_a2","q6_a3","q6_a4",2),
48
+
49
+ ( 7,"q07_text","q07_a1","q07_a2","q7_a3","q7_a4",1),
50
+
51
+ ( 8,"q08_text","q08_a1","q08_a2","q8_a3","q8_a4",3),
52
+
53
+ ( 9,"q09_text","q09_a1","q09_a2","q9_a3","q9_a4",4),
54
+
55
+ (10,"q10_text","q10_a1","q10_a2","q0_a3","q0_a4",4),
56
+
57
+ (11,"q11_text","q11_a1","q11_a2","q1_a3","q1_a4",1);
58
+
59
+
60
+
61
+ create table user(uid int primary key,uname varchar(20));
62
+
63
+ insert into user values(1,'u1'),(2,'u2'),(3,'u3');
64
+
65
+
66
+
67
+ create table user_question(uqid int primary key auto_increment,uid int not null,qid int not null,correct tinyint default 0,unique(uid,qid));
68
+
69
+ insert into user_question(uid,qid,correct) values
70
+
71
+ (1,1,1),
72
+
73
+ (1,2,1),
74
+
75
+ (1,3,1),
76
+
77
+ (1,4,0),
78
+
79
+ (1,5,0),
80
+
81
+ (1,6,0),
82
+
83
+ (2,1,1),
84
+
85
+ (2,2,0),
86
+
87
+ (2,3,1),
88
+
89
+ (2,4,0),
90
+
91
+ (2,5,1),
92
+
93
+ (2,6,0);
94
+
95
+ ```
96
+
97
+ だとします。
98
+
99
+ 上記条件からuid=1の場合qid=1,2,3を除くもの、uid=2の場合qid=1,3,5を除くもの
100
+
101
+ uid=3の場合すべてが対象になります。
102
+
103
+
104
+
105
+ # 抽出
106
+
107
+ ```SQL
108
+
109
+ select * from question as t1
110
+
111
+ where not exists(select 1 from user_question where qid=t1.qid and uid=1 and correct=1)
112
+
113
+ ```
114
+
115
+ uid=xxのxxを指定すれば間違っていたものおよび未回答の物が表示されます。
116
+
117
+
118
+
119
+ 1問だけ表示したいならorder by とlimitを併用します
120
+
121
+ ```SQL
122
+
123
+ select * from question as t1
124
+
125
+ where not exists(select 1 from user_question where qid=t1.qid and uid=1 and correct=1)
126
+
127
+ order by rand()
128
+
129
+ limit 1
130
+
131
+ ```