高级MySQL查询,第1组列

if this is possible

i have in base like this:

AAA 1
AAA 2
BBB 1
BBB 2
BBB 3

and result must be like this

AAA 1   2   
BBB 1   2   3

or

AAA 1,2 
BBB 1,2,3

tnx

Use GROUP_CONCAT.

Query

select column1,
group_concat(column2 separator ',') as column2
from tableName
group by column1;

Result

+---------+---------+
| column1 | column2 |
+---------|---------+
| AAA     | 1,2     |
| BBB     | 1,2,3   |
+---------+---------+

SQL Fiddle

If you want to separate with space( ) instead of comma(,),
then specify separator ' ' in the group_concat.

Then the query would be something like below:

select column1,
group_concat(column2 separator ' ') as column2
from tableName
group by column1;

Result

+---------+---------+
| column1 | column2 |
+---------|---------+
| AAA     | 1 2     |
| BBB     | 1 2 3   |
+---------+---------+

Read more about group_concat here

UPDATE

If you need the each column2 value in separate columns,
then you may need to execute a dynamic sql query.

Query

set @query = null;
select
group_concat(distinct
    concat(
      'max(case when column2 = ''',
      column2, ''' then column2 end) as Value_',column2
    )
  ) into @query
from tableName ;

set @query = concat('select column1, ', @query, ' from tableName 
                  group by column1
');

prepare stmt from @query;
execute stmt;
deallocate prepare stmt;

Result

+---------+---------+---------+---------+
| column1 | Value_1 | Value_2 | Value_3 |
+---------+---------+---------+---------+
| AAA     |  1      |  2      | (null)  |
| BBB     |  1      |  2      | 3       |
+---------+---------+---------+---------+

SQL Fiddle