MySQL TRIM SELECT结果输出

时间:2014-12-12 16:35:45

标签: mysql sql select trim

我有以下MySQL个查询结果,我只想在@之前只有UserName第一部分需要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)

1 个答案:

答案 0 :(得分:2)

最简单的方法是使用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);