MySQL中concat_ws函数应用实例

来源:互联网 发布:美国留学知乎 编辑:程序博客网 时间:2024/04/30 02:14

本文通过实例介绍了MySQL中的concat_ws函数的使用方法,比如select concat_ws(',','11','22','33')。
MySQL中concat_ws函数
使用方法: 
CONCAT_WS(separator,str1,str2,...)

CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。
注意:
如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。

如连接后以逗号分隔 
mysql> select arTitle,concat_ws(';',content,createTime,siteID,id) as content from post where uCenterUId = 4

+-------------------------------+
| concat_ws(';',content,createTime,siteID,id) |
+-------------------------------+
| MjM0MzU=;2017-06-13 11:44:22;hxhslm_20170523;3710 |
+-------------------------------+
1 row in set (0.00 sec)

和MySQL中concat函数不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL 
mysql> select concat_ws(',','11','22',NULL);
+-------------------------------+
| concat_ws(',','11','22',NULL) |
+-------------------------------+
| 11,22 |
+-------------------------------+
1 row in set (0.00 sec)



一对多的分组查询实现:


当查询数据是一对多的时候,我们需要组装的数据格式是一个标题对应多个字段内容的时候,我们需要将多个字段合并到一个字段中(如下图)




这种就需要使用一下sql实现查询:

select arTitle,group_concat(concat_ws(';',content,createTime,siteID,id)) as content from post where uCenterUId = 4 and isdisplay = 0 group by articleID order by createTime desc

数据查询结果如下截图(content字段中每个数据是用,分割,每个字段是用;分割)





将查询的数据需要在后台进行组装,以方便在也面展示:


封装demo:

    /* 获取用户评论列表(App接口)     *  参数     *  用户id uCenterUId       *  第几页: pno       * */    public void getUserPostApp(){    //获取用户id参数    /*int uCenterUId = getParaToInt(0);    int pno = getParaToInt(1);*/    String uCenterUId = getPara("uCenterUId");    int pno = getParaToInt("pno");    //根据用户id分页查询该用户对应的文章信息与文章标题    //String sql = "select arTitle,group_concat(concat_ws(';',content,createTime,siteID)) as content from post group by articleID LIMIT 0,8;";    //Db.paginate();    Page<Record> postPage = Db.paginate(pno,8, "select arTitle,group_concat(concat_ws(';',content,createTime,siteID,id)) as content","from post where uCenterUId = ? and isdisplay = 0 group by articleID order by createTime desc",uCenterUId);    PostApp postApp = new PostApp();    List<Record> list = postPage.getList();    pageApp pap =null;    List aa = new ArrayList<PostApp>();         if(null!=list&&list.size()>0){          for(int i=0;i<list.size();i++){     String content = list.get(i).get("content");     String[] content1 = content.split(",");     List<PostA> postAList = new ArrayList<>();     PostA postA = null;     for(int j = 0;j<content1.length;j++){     String[] content2 =content1[j].split(";");     if(content2.length==4){     postA = new PostA();         postA.setContent(Base64Utils.decodeData(content2[0]));         postA.setTime(content2[1]);         postA.setSiteId(content2[2]);         postA.setPostId(Integer.parseInt(content2[3]));         postAList.add(postA);     }     }     Map map = new HashMap<String,List>();     map.put("arTitle", list.get(i).get("arTitle"));     map.put("List", postAList);     aa.add(map);     }     }    int  pageNumber =  postPage.getPageNumber();    int  pageSize =  postPage.getPageSize();    int  totalPage =  postPage.getTotalPage();    int  totalRow =  postPage.getTotalRow();    pap = new pageApp<>(aa, pageNumber, pageSize, totalPage, totalRow);        getResponse().setHeader("Access-Control-Allow-Origin", "*");getResponse().setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");getResponse().setHeader("Access-Control-Max-Age", "3600");getResponse().setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With");renderJson(pap);


获得数据的样例:


{  "lastPage": true,  "pageSize": 8,  "pageNumber": 1,  "list": [    {      "arTitle": "袭击事件频发 欧洲安全形势不容乐观",      "List": [        {          "content": "…`",          "time": "2017-06-13 13:40:13",          "siteId": "1",          "postId": 3728        }      ]    },    {      "arTitle": "青岛面条鱼获丰再现鱼满舱",      "List": [        {          "content": "rrr",          "time": "2017-06-13 13:31:11",          "siteId": "1",          "postId": 3726        }      ]    },    {      "arTitle": "南非骚乱波及多家华人店铺 中国使馆提醒注意安全",      "List": [        {          "content": "图图他",          "time": "2017-06-13 11:16:45",          "siteId": "1",          "postId": 3703        },        {          "content": "太阳雨u",          "time": "2017-06-13 14:52:12",          "siteId": "1",          "postId": 3759        },        {          "content": "发挥好回家",          "time": "2017-06-13 14:52:17",          "siteId": "1",          "postId": 3760        }      ]    },    {      "arTitle": "美国海关启动最严打假 中国货品是主要目标",      "List": [        {          "content": "123",          "time": "2017-06-13 09:29:37",          "siteId": "1",          "postId": 3673        },        {          "content": "123",          "time": "2017-06-13 09:45:35",          "siteId": "1",          "postId": 3674        },        {          "content": "123",          "time": "2017-06-13 09:46:07",          "siteId": "1",          "postId": 3675        },        {          "content": "123",          "time": "2017-06-13 09:46:12",          "siteId": "1",          "postId": 3676        },        {          "content": "123",          "time": "2017-06-13 09:46:21",          "siteId": "1",          "postId": 3677        },        {          "content": "123",          "time": "2017-06-13 09:48:35",          "siteId": "1",          "postId": 3678        },        {          "content": "123",          "time": "2017-06-13 09:48:38",          "siteId": "1",          "postId": 3679        },        {          "content": "123",          "time": "2017-06-13 09:48:40",          "siteId": "1",          "postId": 3680        },        {          "content": "123",          "time": "2017-06-13 09:53:15",          "siteId": "1",          "postId": 3682        },        {          "content": "123",          "time": "2017-06-13 10:05:00",          "siteId": "1",          "postId": 3685        },        {          "content": "123",          "time": "2017-06-13 10:11:51",          "siteId": "1",          "postId": 3686        },        {          "content": "日日日",          "time": "2017-06-13 10:12:29",          "siteId": "1",          "postId": 3688        },        {          "content": "图图他",          "time": "2017-06-13 10:19:46",          "siteId": "1",          "postId": 3692        },        {          "content": "人人让她",          "time": "2017-06-13 10:35:13",          "siteId": "1",          "postId": 3695        },        {          "content": "rryyy",          "time": "2017-06-13 11:56:30",          "siteId": "1",          "postId": 3719        },        {          "content": "rryyy",          "time": "2017-06-13 11:56:34",          "siteId": "1",          "postId": 3720        },        {          "content": "rryyy",          "time": "2017-06-13 11:56:37",          "siteId": "1",          "postId": 3721        },        {          "content": "热饿",          "time": "2017-06-13 14:51:37",          "siteId": "1",          "postId": 3758        },        {          "content": "关羽",          "time": "2017-06-13 16:41:18",          "siteId": "1",          "postId": 3779        }      ]    },    {      "arTitle": "华裔候选人陈文雄法国议会选举首轮投票大幅胜出",      "List": [        {          "content": "分割哈哈",          "time": "2017-06-13 08:51:44",          "siteId": "1",          "postId": 3667        }      ]    },    {      "arTitle": "可以改变世界,但“中国吃货”不背锅!",      "List": [        {          "content": "嗯嗯哒",          "time": "2017-06-12 19:36:43",          "siteId": "1",          "postId": 3648        },        {          "content": "让他英语",          "time": "2017-06-12 19:36:48",          "siteId": "1",          "postId": 3649        },        {          "content": "让体育UIi",          "time": "2017-06-12 19:36:53",          "siteId": "1",          "postId": 3650        },        {          "content": "天谕",          "time": "2017-06-12 19:36:58",          "siteId": "1",          "postId": 3651        },        {          "content": "分割哈哈",          "time": "2017-06-13 14:13:03",          "siteId": "1",          "postId": 3739        },        {          "content": "如图ui",          "time": "2017-06-13 14:13:07",          "siteId": "1",          "postId": 3740        },        {          "content": "体育局",          "time": "2017-06-13 14:13:10",          "siteId": "1",          "postId": 3741        },        {          "content": "雨ii",          "time": "2017-06-13 14:13:15",          "siteId": "1",          "postId": 3742        },        {          "content": "让她雨ii哦",          "time": "2017-06-13 14:13:19",          "siteId": "1",          "postId": 3743        }      ]    },    {      "arTitle": "南非布隆方丹华人店铺遭暴民哄抢 商铺暂停营业",      "List": [        {          "content": "地负海涵",          "time": "2017-06-12 19:34:22",          "siteId": "1",          "postId": 3647        }      ]    },    {      "arTitle": "中国女足第一美女是谁?",      "List": [        {          "content": "额",          "time": "2017-06-12 19:17:00",          "siteId": "1",          "postId": 3646        }      ]    }  ],  "firstPage": true,  "totalRow": 8,  "totalPage": 1}









原创粉丝点击