Found a nice fairy giant function, GROUP_CONCAT, it was conquered

I now have such a table

TABLE `test_group_concat` the CREATE ( 
  ` id` BIGINT (20 is) the AUTO_INCREMENT the NOT NULL, 
  `user_id` BIGINT (20 is) the COMMENT the NOT NULL 'user ID', 
  ` sku_id` BIGINT (20 is) the COMMENT the NOT NULL 'number of goods purchased by the user' , 
  `create_time` the NOT NULL the DEFAULT CURRENT_TIMESTAMP the COMMENT datetime 'created', 
  ` update_time` the NOT NULL the DEFAULT CURRENT_TIMESTAMP the COMMENT datetime 'update', 
  a PRIMARY KEY ( `id`) 
) =. 9 ENGINE = the InnoDB the AUTO_INCREMENT the DEFAULT the CHARSET = UTF8;

  Each user needs to find out all the goods purchased

SELECT
	user_id,
	sku_id
FROM
	test_group_concat

  Check out the result is this, is not it relatively ugly

 

I want to show each user only once, it can only group by, but we all know that group by a grouping function, non-grouping columns can only display one, that if I want to have all the sku_id check out how to do it

We can use this function immortal

SELECT
	user_id,
	GROUP_CONCAT(sku_id) sku_list
FROM
	test_group_concat
GROUP BY
	user_id;

  The result becomes so

 

 

 Is not it amazing ah ~~~~~

 

Guess you like

Origin www.cnblogs.com/zhangqian27/p/12622900.html