欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mysql 行转列,列转行

程序员文章站 2022-05-29 08:32:53
...
列转行
select a.id,
       a.name,
       a.province_name,
       a.city_name,
       a.area_name,
       a.contact,
       a.mphone,
       a.address,
       a.qq,
       a.type_id,
       a.typename
  from(
SELECT t1.*, GROUP_CONCAT(t2.name) as typename
  FROM tos_sys_partner t1
  LEFT JOIN tos_goods_type t2 ON FIND_IN_SET(t2.id, t1.type_id)
 GROUP BY t1.id
 ORDER BY t1.id ASC) a

 

行转列
SELECT
	*
FROM
	sys_role AS r,
	(
		SELECT
			a.role_id,
			group_concat(a.dept_id) AS '部门'
		FROM
			`sys_role_dept` a
		GROUP BY
			role_id
	) AS temp
WHERE
	r.role_id = temp.role_id;