MYSQL:将 GROUP BY 与字符串文字一起使用
我有包含这些列的下表:
shortName, fullName, ChangelistCount
有没有办法通过其全名中的字符串文字对它们进行分组?全名代表文件目录,因此我想显示某些父文件夹而不是单个文件的结果。
我尝试了以下内容:
GROUP BY fullName like "%/testFolder/%" AND fullName like "%/testFolder2/%"
但是它只按第一场比赛真正分组......
谢谢!
I have the following table with these columns:
shortName, fullName, ChangelistCount
Is there a way to group them by a string literal within their fullName? The fullname represents file directories, so I would like to display results for certain parent folders instead of the individual files.
I tried something along the lines of:
GROUP BY fullName like "%/testFolder/%" AND fullName like "%/testFolder2/%"
However it only really groups by the first match....
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您想要这样的东西:
要理解的关键思想是像
fullName LIKE foo AND fullName LIKE bar
这样的表达式是整个表达式必须计算为TRUE
或 < code>FALSE,所以你只能从中得到两个组。使用
IF
表达式返回几个不同值之一将使您获得更多组。请记住,这不会特别快。如果您有一个非常大的数据集,您应该探索其他存储数据的方法,这些方法不需要
LIKE
比较来进行分组。Perhaps you want something like:
The key idea to understand is that an expression like
fullName LIKE foo AND fullName LIKE bar
is that the entire expression will necessarily evaluate to eitherTRUE
orFALSE
, so you can only get two total groups out of that.Using an
IF
expression to return one of several different values will let you get more groups.Keep in mind that this will not be particularly fast. If you have a very large dataset, you should explore other ways of storing the data that will not require
LIKE
comparisons to do the grouping.您必须使用子查询来派生您想要最终分组的列值:
You'd have to use a subquery to derive the column values you'd like to ultimately group on:
使用“when/then”条件或使用另一个临时表,其中包含您希望查找和分组的所有匹配项。来自我的数据库的样本。
在这里,我想根据地址字段内的城市对所有用户进行分组。
Either use when/then conditions or Have another temporary table containing all the matches you wish to find and group. Sample from my database.
Here I wanted to group all users based on their cities which was inside address field.