Dima Dz
Dima Dz

Reputation: 538

Mysql concatenate only specific rows

In Mysql, I have the following table:

id | paramname   | paramcategory     | value           |
---+-------------+-------------------+-----------------+
1  | width       | dimensions        | 240             |
2  | height      | dimensions        | 400             |
3  | param1      | category1         | some value 1    |
4  | param2      | category1         | some value 2    |
5  | param3      | category10        | some value 100  |
...

I'd like to have a query that will return a table with only several rows concatenated, and all other rows should remain intact, something like this:

paramname     | value        |
--------------+--------------+
width, height | 240 x 400    |
param1        | some value 1 |
...

I'm thinking about concatenating based on the needed paramcategory, but if possible/needed, concatenation can happen for specific paramnames as well. Whatever is easier/simpler.

Any help please?

Upvotes: 0

Views: 59

Answers (1)

Johnny Ox Harvester
Johnny Ox Harvester

Reputation: 36

Looking at this problem from above, you are going to have to 'UNION' 2 queries together. The first part of the union is your concat'd results, the second your original rows. For the first part you are going to need to do a self join on this table, along the lines of

select concat(a.paramname, b.paramname), concat(a.value, b.value) from table a, table b where a.paramcategory = b.paramcategory

along those lines....

Actually if you swap the 2 parts of the union around, you'll keep the original column names too.

Upvotes: 2

Related Questions