mysql 解决一列数据vs多列数据 合并为一列数据

场景如下,mysql根据用户u_id查询课程,不过该用户的课程为多列数据

table users

 table curriculums

查询语句
select * from users as u join curriculums as c on c.u_id = u.u_id where c.u_id = 1

result

发现结果是多列数据,需求为一条数据返回,得到sql函数方法 CONCAT() 和 GROUP_CANCAT()

select u.u_id,u.nickename,CONCAT("[",GROUP_CONCAT('{"curr":"',c.c_name,'","price":',c.price,',"info":"',c.info,'"}'),"]") as datas 
                from users as u 
                join curriculums as c on c.u_id = u.u_id 
                where c.c_name like '%o%' group by u.u_id

 result

 

CONCAT() 和 GROUP_CANCAT() 都是把结果拼接为一条字符串,上面的例子演示为拼接为json字符串


如果业务需求减少查询次数并一次查询多个用户的多个数据

golang的gorm框架操作可以制作以下结构体scan为数据

type users struct {
     Uid int `gorm:"column:u_id" json:"uid"`
     ......    
}

type curriculums struct {
     Cid int `gorm:"column:c_id" json:"cid"`
     Cname string `gorm:"column:c_name" json:"title"`
     Uid int `gorm:"column:u_id" json:"uid"`
     Price float64   `gorm:"column:price" json:"price"`
}

type result struct {
     users 
     Datas string   `gorm:"column:datas" json:"datas,omitempty"`    # gorm:"column:datas" 为CONCAT() or GROUP_CANCAT() as 之后的名称

# 需要对其中数据进行操作才用json反序列化操作Datas列数据为curriculums结构体,sql查询时没有对其操作
   Cu curriculums `json:"curriculums"` }

var datas []result
db.Raw("sql语句 ... CONCAT() ...").Scan(&datas)

将数据scan到结构体中,其中result.Datas 含有的数据为这次在mysql中拼接的字符串,可以对其进行json反序列化操作

 但是返回数据为json格式字符串了,如果无需其他操作可直接由一些框架返回 datas 切片结构体数据

其中注意点为: 

1.查询用户id为1,其中该列数据admin_del为null,得到结果CONCAT 和 GROUP_CANCAT 拼接都会返回 空字符串

2.GROUP_CANCAT() 函数中可以进行另外的查询

 3.CONCAT和GROUP_CANCAT应用场景不同

例如一个用户的多个课程查询,如果使用 CONCAT 与一般查询一般,返回的数据为多列

 而GROUP_CANCAT则不同,会将多个列的结果拼接为一个字符串,而不是像join users 返回多列数据

上面的成功例子则内部使用GROUP_CANCAT先对多个数据列拼接,外部使用 [] 拼接为json格式

 4.updata 中也能使用

update curriculums set info = CONCAT(c_name,"----") where c_id = 184

-----------------------------------------------------------分割线-------------------------------------------------------------------

本笔记自用 而已,如有错误请指正!

原文地址:https://www.cnblogs.com/zengxm/p/13473340.html