这就是我的表格:
表名:关注
+----+-------+-------+-------+-------+
| ID | user1 | user2 | user3 | user4 |
+----+-------+-------+-------+-------+
| 1 | user2 | user1 | user2 | user2 |
| 2 | user3 | user3 | user1 | user3 |
| 3 | user4 | user4 | user4 | user1 |
+----+-------+-------+-------+-------+
但如果改变一个值,如:
+----+-------+-------+-------+-------+
| ID | user1 | user2 | user3 | user4 |
+----+-------+-------+-------+-------+
| 1 | user2 | user1 | user2 | user2 |
| 2 | user3 | user3 | null | user3 |
| 3 | user4 | user4 | user4 | user1 |
+----+-------+-------+-------+-------+
如何使值user4
(及其下面的其他值)上升,两者之间没有 NULL 值?
我希望输出为:
+----+-------+-------+-------+-------+
| ID | user1 | user2 | user3 | user4 |
+----+-------+-------+-------+-------+
| 1 | user2 | user1 | user2 | user2 |
| 2 | user3 | user3 | user4 | user3 |
| 3 | user4 | user4 | null | user1 |
+----+-------+-------+-------+-------+
我厌倦了这个问题:
UPDATE following t1
LEFT OUTER JOIN following t2 ON T1.user3+1=T2.user3 - 2
SET t1.user3 = t2.user3;
但是我收到了一些错误Error code 1,292, SQLState 22007] Truncated incorrect DOUBLE value: user2
答案 0 :(得分:0)
我不知道如何在mysql中编写查询,但这里有一种方法可以实现它。
拳头:
You show get the Min(ID) and Max(ID).
第二:
Write a loop For i = Min(ID) to Max(ID)
第三:
Update Following set user3 = (Select user3 from Following where ID = i+1) where ID = i
第四:
Set the last one row data to null or something else what you want init.
答案 1 :(得分:0)
您需要创建一个包含可用非空名称的有序列表
UPDATE Table1
LEFT JOIN ( SELECT `ID`,
`user3`,
@rn := @rn + 1 as rn
FROM Table1
CROSS JOIN ( SELECT @rn := 0 ) as vars
WHERE `user3` IS NOT NULL
ORDER BY `ID` ) news
ON Table1.`ID` = news.rn
SET Table1.`user3` = news.`user3`;
<强>输出强>
注意:这里我假设ID
是连续序列。如果不是这种情况,您需要创建一个时间id列来进行连接。