ITPub博客

首页 > 数据库 > MySQL > mysql常用函数--个人笔记

mysql常用函数--个人笔记

原创 MySQL 作者:小亮520cl 时间:2015-07-29 15:49:37 0 删除 编辑

----ifnull   isnull函数


SELECT * FROM t3;
id name
1
2
3  NULL


SELECT IFNULL(NAME,'kkkk') FROM t3;




if函数

点击(此处)折叠或打开

  1. mysql> select * from t3;
    +------+------+
    | id   | cj   |
    +------+------+
    |    1 |    2 |
    |    2 |    3 |
    |    3 |    3 |
    |    4 |    5 |
    |    5 |    5 |
    |    6 |    7 |
    +------+------+
    6 rows in set (0.00 sec)


    mysql> select *,if(id=cj,'eq','neq') from t3;
    +------+------+----------------------+
    | id   | cj   | if(id=cj,'eq','neq') |
    +------+------+----------------------+
    |    1 |    2 | neq                  |
    |    2 |    3 | neq                  |
    |    3 |    3 | eq                   |
    |    4 |    5 | neq                  |
    |    5 |    5 | eq                   |
    |    6 |    7 | neq                  |
    +------+------+----------------------+



substring_index函数

  1. MySQL> select * from t1;
    +--------+---------------------+
    | userid | atime |
    +--------+---------------------+
    | 1 | 2013-08-12 11:05:25 |
    | 2 | 2013-08-12 11:05:29 |
    | 3 | 2013-08-12 11:05:32 |
    | 5 | 2013-08-12 11:05:34 |
    | 1 | 2013-08-12 11:05:40 |
    | 2 | 2013-08-12 11:05:43 |
    | 3 | 2013-08-12 11:05:48 |
    | 5 | 2013-08-12 11:06:03 |
    +--------+---------------------+
    8 rows in set (0.00 sec)


  2. 其中userid不唯一,要求取表中每个userid对应的时间离现在最近的一条记录.初看到一个这条件一般都会想到借用临时表及添加主建借助于join操作之类的.
    给一个简方法:
  3. MySQL> select userid,substring_index(group_concat(atime order by atime desc),",",1) as atime from t1 group by userid;
    +--------+---------------------+
    | userid | atime |
    +--------+---------------------+
    | 1 | 2013-08-12 11:05:40 |
    | 2 | 2013-08-12 11:05:43 |
    | 3 | 2013-08-12 11:05:48 |
    | 5 | 2013-08-12 11:06:03 |
    +--------+---------------------+
    4 rows in set (0.03 sec)

  4. substring_index(col_name,"分隔符",n)    ----n表示前n部分

字符串出现的次数
  1. (root@127.0.0.1) [test]> select * from city;
  2. +-----------------------------------------+
  3. | name |
  4. +-----------------------------------------+
  5. | 北京市北京市北京北京北京市 |
  6. | tom is a good tom boy |
  7. +-----------------------------------------+
  8. 2 rows in set (0.00 sec)
查看name中tom出现的次数
(root@127.0.0.1) [test]> select (length(name)-length(replace(name,"tom","")))/length("tom")  as count ,name from city;
+--------+-----------------------------------------+
| count  | name                                    |
+--------+-----------------------------------------+
| 0.0000 | 北京市北京市北京北京北京市              |
| 2.0000 | tom is a good tom boy                   |
+--------+-----------------------------------------+
2 rows in set (0.00 sec)

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29096438/viewspace-1755476/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
毕业以后专业任职数据库工程师职位,itpub一直作为自己的笔记记录的地方,blog写的不详细,草书笔记,仅供参考!

注册时间:2013-09-12

  • 博文量
    529
  • 访问量
    1126984