`

MySQL的substring_index函数

 
阅读更多

MySQL的substring_index函数

假定有一张表,其中有个字段是 email 保存了电子邮箱,然后,我想统计每种邮箱的占比情况。

我们知道电子信箱的格式是: account@domain ,要实现上面的统计,必须取得 email 字符串中 @ 后面的域名部分。

MySQL提供了一个名为 substring_index 的函数,能够满足我们的需求:

MySQL 5.5 Reference Manual 写道
SUBSTRING_INDEX(str,delim,count)

Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
        -> 'www.mysql'
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);
        -> 'mysql.com'

This function is multi-byte safe. 
 

查询语句如下:

select
    substring_index(email,'@',-1) as domain,
    count(distinct email) as count,
    count(distinct email)
        / (select count(distinct email) from qzt_resume_basic where email like '%@%')
        * 100 as percent
from qzt_resume_basic
where email like '%@%' 
group by 1 
order by 2 desc 
limit 10;
 

 

2
0
分享到:
评论

相关推荐

    mysql截取的字符串函数substring_index的用法

    主要介绍了mysql截取的字符串函数substring_index的用法,需要的朋友可以参考下

    mysql截取函数常用方法使用说明

    常用的mysql截取函数有:left(), right(), substring(), substring_index() 下面来一一说明一下: 1.左截取left(str, length) 2.右截取right(str, length) 3.substring(str, pos); substring(str, pos, len) 4....

    mysql(5.6及以下)解析json的方法实例详解

    mysql(5.6及以下)解析json #json解析函数 DELIMITER $$ DROP FUNCTION IF EXISTS `json_extract_c`$$ CREATE FUNCTION `json_extract_c`( details TEXT, required_field VARCHAR (255) ) RETURNS TEXT CHARSET ...

    深入SQL截取字符串(substring与patindex)的详解

    首先学习两个函数1.substring 返回字符、binary、text 或 image 表达式的一部分。基本语法:SUBSTRING ( expression , start , length ) expression:字符串、二进制字符串、text、image、列或包含列的表达式start:...

    MySQL将一个字段中以逗号分隔的取出来形成新的字段实现

    substring_index(substring_index(a.related_shop_ids,',' ,b.help_topic_id+1),',',-1) shopid from sales_hang_account a join mysql.help_topic b on b.help_topic_id < (length(a.related_shop_ids) -...

    MySQL截取和拆分字符串函数用法示例

    本文实例讲述了MySQL截取和拆分字符串函数用法。分享给大家供大家参考,具体如下: 首先说截取字符串函数: SUBSTRING(commentid,9) 这个很简单,从第9个字符开始截取到最后。SUBSTRING的参数有三个,最后一个是...

    Mysql字符串截取及获取指定字符串中的数据

    前言:本人遇到一个需求,需要在MySql的字段中截取一段字符串中的特定字符,类似于正则表达式的截取,苦于没有合适的方法,百度之后终于找到一个合适的方法:substring_index(‘www.sqlstudy.com.cn’, ‘.’, -2) ...

    MySQL中将一列以逗号分隔的值行转列的实现

    前言 有时会遇到没有遵守第一范式设计模式的业务表。即一列中存储了多个属性值。...select distinct(substring_index(substring_index(a.col,',',b.help_topic_id+1),',',-1)) from (select group_concat

    Mysql通过存储过程分割字符串为数组

    SUBSTRING_INDEX(str,delim,count) 返回从字符串str的第count个出现的分隔符delim之后的子串。如果count是正数,返回最后的分隔符到左边(从左边数) 的所有字符。如果count是负数,返回最后的分隔符到右边的所有字符...

    MySQL笔记之字符串函数的应用

    字符串操作在程序设计中是非常重要的组成部分,而MySQL数据库中的字符串操作却相当简单 需要注意的是:下面所有的函数只是将修改后的结果当查询返回,均不对原数据...SUBSTRING_INDEX截取字符串 代码如下:mysql> SELE

    Windows提权总结(一)——数据库与系统漏洞.pdf

    导⼊/导出成功 Step2: 查看plugin的值 select Host,user,plugin from mysql.user where user = substring_index(user(),'@',1); 当plugin值为 mysql_native_password 时可通过账户连接提权 Step3: 查看系统架构以及...

    mysql基础只是总结

    【mysql中的简单的数学函数】 1、统计函数 count()|max()|min()|avg()|sum() 2、mysql运算 php运算一致(除逻辑运算&& || !) 3、数学函数 pi()|round()|rand()|ceil()|floor|sin()|abs()|mod() 4、特殊的 ...

    IP处理函数inet_aton()和inet_ntoa()使用说明

    MYSql的IP对int的转换函数 select inet_aton(ip) from table_name; 网络地址: 192.168.33.123 每一个值最大不会越过255,也就是十六进制的FF,两个Byte刚好表示的最大值是255, 这样子,就可以用一个32位的整形来...

    API精灵源码(C#编)

    private string mysql ;//查询语句 private string sql_update; private System.Windows.Forms.RichTextBox tipsmemo; private System.Windows.Forms.TextBox mat_text; private OleDbCommand mycommand =...

    SQL语句大全 珍藏版2019-02-28

    (22) Substring ...................................................... ............... ...............13 取字符 SUBSTR(str,pos) SUBSTR(str,pos,len) (23) TRIM ...... .............. ........................

    Excel POI读取封装(文件+示范代码)

    src = src.substring(0, src.indexOf(".")); obj = Integer.valueOf(src); } } if (classzz == Double.class) obj = Double.valueOf(src); if (classzz == String.class || classzz == char.class) obj = ...

    JAVA WEB 开发详解:XML+XSLT+SERVLET+JSP 深入剖析与实例应用.part2

    8.5 mysql对中文的处理 302 8.6 小结 302 第9章 会话跟踪 303 9.1 用于会话跟踪的技术 303 9.1.1 ssl会话 304 9.1.2 cookies 304 9.1.3 url重写 305 9.2 java servlet api的会话跟踪 306 9.2.1 httpsession...

Global site tag (gtag.js) - Google Analytics