我正在研究以下mysql:
select num, @record,
case
when @record = num then @count:=@count+1
when @record <> @record:=num then @count:=1 end as n
from
Logs ,(select @count:=0,@record:=(SELECT num from Logs limit 0,1)) r
“日志”表如下所示:
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
查询的输出类似于:
|num | @record | n |
----------------------
| 1 | "1" | 1.0 |
| 1 | "1" | 2.0 |
| 1 | "1" | 3.0 |
| 2 | "1" | 1.0 |
| 1 | "2" | 1.0 |
| 2 | "1" | 1.0 |
| 2 | "2" | 2.0 |
对于第二行和第三行,我很难理解如何得出。例如,在row_1(Id = 1)@record = Num
中,为什么n = 1而不是2?
在row_3 @record = Num
中,为什么n = 3而不是2?
只有一个@record全局变量吗?还是每个数字都有自己的@record变量?
有人可以帮我理解@sql变量逻辑吗?谢谢!
答案 0 :(得分:1)
MySQL在处理任何行之前首先评估JOIN,即Path tempDir = Files.createTempDirectory("tempfiles");
String tempFile = tempDir.toString() + "/json";
dataset.coalesce(1).write().json(tempFile);
Path jsonFile = Files.find(Paths.get(tempFile), 1, (path, basicFileAttributes) -> {
return Files.isRegularFile(path) && path.toString().endsWith("json");
}).findFirst().get();
BufferedReader reader = Files.newBufferedReader(jsonFile);
reader.lines().forEach(System.out::println);
和@count=1
。第一行的@record=1
与num
相同,因此@record
的值为1(n
)。
第二行和第三行遵循相同的逻辑结果值2和3。
第四行重置@count+1
并将新的@count
放入@record
以此类推...
答案 1 :(得分:0)
第n列的值来自@count,而不是@record。 @record用于在更改num时将@count重置为1