回答編集履歴

1

update

2020/11/13 16:23

投稿

yambejp
yambejp

スコア116732

test CHANGED
@@ -1,14 +1,18 @@
1
- 仮にp_commentsだけで完結して考え
1
+ ※ランクカラムを設定す
2
+
3
+ ```SQL
2
4
 
3
5
  CREATE TABLE p_comments
4
6
 
5
7
  (super_ID int unique,
6
8
 
7
- thread_ID int,
9
+ thread_ID int,
8
10
 
9
11
  up_count int,
10
12
 
11
- down_count int);
13
+ down_count int,
14
+
15
+ rank int null);
12
16
 
13
17
 
14
18
 
@@ -25,3 +29,21 @@
25
29
  (7, 5, 2, 0),
26
30
 
27
31
  (8, 1, 1, 2);
32
+
33
+
34
+
35
+
36
+
37
+ insert into p_comments(super_ID,rank) select super_ID,(
38
+
39
+ select count(*)+1 from p_comments
40
+
41
+ where up_count-down_count-t1.up_count+t1.down_count<0 or
42
+
43
+ (up_count-down_count-t1.up_count+t1.down_count=0 and super_ID<t1.super_ID) ) as rank
44
+
45
+ from p_comments as t1
46
+
47
+ on duplicate key update rank=values(rank)
48
+
49
+ ```