试图了解如何组合MariaDB查询

时间:2017-05-11 22:40:47

标签: mysql mariadb

上下文

我想弄清楚如何创建一个组合的sql语句,该语句将总和大小并计算表中file列中保存的特定卷路径中的文件数。路径是从根卷一直到文件位置的绝对路径。一些音量路径有超过400个字符,包括空格和特殊字符。我希望根据目标位置中的文件夹列表获取列表大小和文件计数。我能够提取文件列表或总和&计算目标文件夹中的所有文件,但不再计算每个子文件夹中更深层次的文件。我正在处理一个平面文件,每行都有这个信息,我使用php将它导入MariaDB,这样我们就可以运行查询并报告给定的信息。

select distinct left(replace(file, '/volume/path/to/target/folder/', '' ),locate("/",replace(file, '/volume/path/to/target/folder/', '' ))) from FileList

输出:

These/
are/
the/
folders/
are/
looking/
for/

乳清试图分组:

select left(replace(file, '/volume/path/to/target/folder/', '' ),locate("/",replace(file, '/volume/path/to/target/folder/', '' ))) as 'folder', sum(realsize), count(file)  from FileList where file like '/volume/path/to/target/folder%' group by 'folder'

输出:

folder, sum(realsize), count(file)
<blank>, 127534970, 2340239024

尝试子查询:

select 
    left(replace(file, '/volume/path/to/target/folder/', '' ),locate("/",replace(file, '/volume/path/to/target/folder/', '' ))) as 'folder'
    count(file),
    sum(realsize)
From
    FileList
Where
    folder in (select distinct left(replace(file, '/volume/path/to/target/folder/', '' ),locate("/",replace(file, '/volume/path/to/target/folder/', '' ))) from FileList)

输出:

folder, sum, count
*NULL*,127534970,*NULL*

问题:

组合以获得我正在寻找的输出有什么问题?

预期产出:

(random number to simulate output)
folder, sum, count
These/, 1245, 234
are/  , 2345, 345
the/  , 8675, 456
folders/, 345, 34
are/  , 9876, 123
looking/, 456, 2
for/, 2346890, 987

0 个答案:

没有答案