0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks johnny, i don't quite see the following. your query will most likely concatenate all similar categories, whereas i need to concatenate only 1, say dimensions. how would i do this?
something along the lines of select paramname, paramcategory from tablename where paramcategory != 'dimensions' UNION select concat_ws(', ', a.paramname, b.paramname), concat_ws(', ', a.value, b.value) from table a, table b where a.paramname != b.paramname and a.paramcategory = b.paramcategory and a.paramcategory = 'dimensions'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.