mysql 行转列 和 列转行

我们有时会将一些数据已逗号的连接方式存在数据库,当取出时我们又想单独一个个取出来

利用help_topic 的自增性

LENGTH(wu.`password`) - length(replace(wu.password,',',''))+1 计算出有多少个数据

join可以循环生成连接 即 一对多

SELECT
     wu.id,ht.help_topic_id,substring_index(substring_index(wu.`password`,',',ht.help_topic_id+1),',',-1) 
FROM
    `web_userinfo` wu
 JOIN mysql.help_topic ht ON (ht.help_topic_id < LENGTH(wu.`password`) - length(replace(wu.password,',',''))+1)
ORDER BY wu.id
;

 

 

 

接下来看看列转行 这就简单多了 比如下面是查询密码为123456的所有人的姓名

 

posted on 2017-01-02 21:20  pytown  阅读(215)  评论(0)    收藏  举报

导航