简体   繁体   English

SQL选择不同的值,如其他值

[英]SQL selecting distinct values like other values

So I have a table that is automatically generated by an instant messenger application and I am looking for all messages sent by all users. 因此,我有一个由即时通讯程序应用程序自动生成的表,并且正在查找所有用户发送的所有消息。 Each conversation has a "to" column the problem is that it appends a short connection string on the end. 每个对话都有一个“ to”列,问题是它在末尾附加了一个短连接字符串。 so the to column looks like: username @company.com/id 1111 所以“收件人”列看起来像: 用户名 @ company.com / id 1111

I am looking to get all distinct usernames and the number of times they appear. 我希望获得所有不同的用户名及其出现的次数。 Any suggestions are appreciated. 任何建议表示赞赏。 I am using PostgreSQL 9.1.1 我正在使用PostgreSQL 9.1.1

The problem is parsing the string. 问题是解析字符串。 This depends highly on the dialect of SQL. 这在很大程度上取决于SQL的方言。 Here is an approach using MySQL: 这是使用MySQL的一种方法:

select substring_index(`to`, '/', 1) as username, count(*)
from t
group by substring_index(`to`, '/', 1);

In most other SQL dialects, the logic would be slightly different. 在大多数其他SQL方言中,逻辑会略有不同。 In SQL Server: 在SQL Server中:

select left("to", charindex('/', "to") - 1) as username, count(*)
from t
group by left("to", charindex('/', "to") - 1);

In other databases, the equivalent of charindex() might be instr() or position() . 在其他数据库中, charindex()的等效项可能是instr()position()

EDIT: 编辑:

I originally miinterpreted the "username" as being the entire email address. 我最初将“用户名”误解为整个电子邮件地址。 Barmar has pointed out that the formatting in the question really suggests the part before the @ . Barmar指出问题中的格式确实暗示了@之前的部分。 Having just the user name portion of the email, without the domain, seems dangerous. 仅包含电子邮件的用户名部分而不包含域似乎很危险。 But, the above works, just by substituting '@' for '/' : 但是,上述工作仅通过用'@'代替'/'

select substring_index(`to`, '@', 1) as username, count(*)
from t
group by substring_index(`to`, '@', 1);

select left("to", charindex('@', "to") - 1) as username, count(*)
from t
group by left("to", charindex('@', "to") - 1);

Is there any commonality/shared characteristic of this 'connection string' ? 此“连接字符串”是否有任何共同点/共同特征?

If so, then is just a question of creating the string manipulation to help you disregard it in your SQL query. 如果是这样,那么仅是创建字符串操作以帮助您在SQL查询中忽略它的问题。

Without knowing the pattern of the connection string, would be hard to supply an answer. 如果不知道连接字符串的模式,将很难提供答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM