mysql中行轉列與列傳行的問題

行轉列:mysql

  •   使用cross join 的方式
  •   使用case-when的方式

 

列轉行:sql

SELECT
	user_name,
	REPLACE (
		substring_index(mobile, ',', a.id),
		char_length(
			substring_index(mobile, ',', a.id - 1)
		) + 1
	),
	',',
	''
) AS mobile
FROM
	tb_sequence a
CROSS JOIN (
	SELECT
		user_name,
		concat(mobile, ',') AS mobile,
		length(mobile) - length(REPLACE(mobile, ',', '')) + 1 size
	FROM
		user1 b
) b ON a.id <= b.size

  以上語句實現的前提是:你須要建立一張tb_sequence序列 表 ,只有一列code

CREATE TABLE tb_sequence (
	id INT auto_increment NOT NULL,
	PRIMARY KEY (id)
)

  

 實現的場景爲:左側的表的內容,裝換爲右側blog

參考說明:慕課網 mysql開發技巧(二)開發

相關文章
相關標籤/搜索