Select all unique emails inside a column filled with data

放肆的年华 提交于 2019-12-12 03:38:58

问题


I have a column's field filled with all kinds of data from a user request (one request per row).

I only need the email addresses from these users' requests, and they are all placed in this column according to the following format:

[...data...] Email: mysql@se.com Phone: [...remaining data...]

I have found this question:

select part of table column data in mysql

Which provides a solution for part of the problem:

SELECT SUBSTR(message,INSTR(message,'Email: ')+7) FROM user_req_log

However, this will return all the unwanted remaining data.

What's the most efficient way to confine the results to the strings I need (email addresses) and, at the same time, ignore duplicates?


回答1:


Based on that question's solution, you may simply do:

SELECT DISTINCT SUBSTR(message,
                       @ini_pos:=INSTR(message,'Email: ')+7,
                       INSTR(message,' Phone:')-@ini_pos)
FROM `user_req_log`

It may not be the most efficient way, but it's fairly similar:

  • use the three parameters of SUBSTRING(str,pos,len);
  • get the positions before and after the email address through INSTR(str,substr) or LOCATE(substr,str)/POSITION(substr IN str).
  • assign a variable for the email's initial position (@ini_pos) and reuse it to subtract from the position where you know it ends;
  • use DISTINCT to return each email only once.


来源:https://stackoverflow.com/questions/32871373/select-all-unique-emails-inside-a-column-filled-with-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!