【问题标题】:MySQL TRIM SELECT result outputMySQL TRIM SELECT 结果输出
【发布时间】:2025-11-04 00:50:02
【问题描述】:

我有以下MySQL 查询结果,我只想要@ 之前的用户名第一部分,它只需要10000013 部分,我想删除example.com 部分,可以通过TRIM 吗?

mysql> SELECT UserName,DAY(AcctStartTime), COUNT(ResponseCode='200') FROM table201412 WHERE UserName='10000013@example.com' GROUP BY DATE(AcctStartTime);
+---------------------------+--------------------+------------------------------+
| UserName                  | DAY(AcctStartTime) | COUNT(ResponseCode='200') |
+---------------------------+--------------------+------------------------------+
| 10000013@example.com |                  1 |                            3 |
| 10000013@example.com |                  2 |                            5 |
| 10000013@example.com |                  3 |                            3 |
+----------------------+--------------------+------------------------------+
10 rows in set (0.00 sec)

我需要以下结果:

+---------------------------+--------------------+------------------------------+
| UserName                  | DAY(AcctStartTime) | COUNT(ResponseCode='200') |
+---------------------------+--------------------+------------------------------+
| 10000013                  |                  1 |                            3 |
| 10000013                  |                  2 |                            5 |
| 10000013                  |                  3 |                            3 |
+---------------------------+--------------------+------------------------------+
10 rows in set (0.00 sec)

【问题讨论】:

  • 如果您打扰RTFM,您会看到不,您不能为此使用trim()

标签: mysql sql select trim


【解决方案1】:

最简单的方法是使用substring_index()

select substring_index(UserName, '@', 1) as EmailName

在您的查询中,这将是:

SELECT substring_index(UserName, '@', 1) as EmailName, DAY(AcctStartTime), COUNT(ResponseCode='200')
FROM table201412
WHERE UserName='10000013@example.com'
GROUP BY DATE(AcctStartTime);

【讨论】: