0

I have a table that I created in the group of clients and ready the products separated by commas using GROUP_CONCAT (product) AS products2. Except that I need the Group's product listing to be each in a column instead of comma separated.

In short I need to list the products the group has put together into a single line.

SELECT customer, GROUP_CONCAT (product) AS products2 FROM sales GROUP BY customer

Table.

    CREATE TABLE IF NOT EXISTS `vendas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `customer` int(11) NOT NULL,
  `product` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Extraindo dados da tabela `vendas`
--

INSERT INTO `vendas` (`id`, `customer`, `product`) VALUES
(1, 10, 2),
(2, 10, 1),
(3, 11, 1),
(4, 11, 2);
  • 2
    Why do you need it in columns? You should be able to handle it in the programming language you are using for your application. – Adder Jun 23 '17 at 15:00
  • 1
    this is called a dynamic pivot. several answers already exist on SO for this. Here's one: https://stackoverflow.com/questions/12598120/mysql-pivot-table-query-with-dynamic-columns – xQbert Jun 23 '17 at 15:00
  • @xQbert Maybe dynamic isn't even needed...but yes this is just a pivot query. – Tim Biegeleisen Jun 23 '17 at 15:01
  • True if there is a Known number of products per customer; but given the table design; looks like dynamic is possible. – xQbert Jun 23 '17 at 15:02
  • As Adder suggests, seriously consider handling issues of data display in application code. – Strawberry Jun 23 '17 at 16:10
  • @xQbert In short I need to list the products the group has put together into a single line. – raio mobile Jun 23 '17 at 16:10
  • @Strawberry Using php with MySQL. As I said above, I only need to display the data of each group on the line – raio mobile Jun 23 '17 at 16:13
  • I do not see how my friend's code from the other question can help me. – raio mobile Jun 23 '17 at 16:26

0 Answers0