tags:

views:

1760

answers:

4

I would like to concatenate column names in a way that the first part of the column name is a string and the second part is a number which is the result of another query.

For example:

SELECT CONCAT('column', mytable.mycolumn) FROM table ...

Can this be done in some way. This way it doesn't give me errors but I don't get the expected result and it seems the concatenation doesn' work.

UPDATE: It seems it can't be done. I solved it by concatenating in PHP which means I use two queries instead of one. The question of (in)sufficient normalization is legitimate but will be asked another time. Thanks for all the input.

+1  A: 

I don't believe you can do this with CONCAT() and CONCAT_WS(). I'd recommend using the langauge you are working with the create the field names. Doing it this way would be pretty scary, depending on where the data in the database came from.

Darryl Hein
could you elaborate on scary? the data to concatenate is an auto_incremented ID (primary).
tharkun
Please revise your question. If the number you're using is an auto_incremented ID then how many columns do you expect to have in your table? I guess I don't understand the question, as I hope this table does not have columns like "column1", "column2, ..., "column90000", ...
Arjan
sort of but not really. the columns are results to single questions, one user per row. so it's a result set of questionnaire results. every question has an id and that id is included in the column name.
tharkun
If it's not too late to change the database design then I'd do so... For now: what about just using "select *" and parse the results elsewhere?
Arjan
maybe I'll try to discuss this one day on SO but I think a full normalization of my design would be suboptimal compared to what I have now. my db is 90% normalized and normalizing that specific part would mean much more complex queries and fetching hundreds of rows instead of one in so many places that I prefer to have one smaller problem which I will solve now just by writing two queries instead of one.
tharkun
Don't your dynamic table definitions (and hence the unlimited number of tables) make future upgrades (or worse: rollbacks of upgrades...) of your application kind of troublesome as well?
Arjan
+3  A: 

This first of all makes me wonder about the database design that requires this...

Just in case it might help you, a different approach:

select case mytable.mycolumn
  when 1 then column1
  when 2 then column2
  when ...
  else ...
  as my_semi_dynamic_column
from ...
Arjan
thanks for the idea. it won't work because it has to be completely generic.
tharkun
Reading your question again, I see you're using numbers. So, I changed the "when 'a' then columna" into "when 1 then column1". Is the number of the 'columnxx' columns in your table really that high...?
Arjan
yes, it's quite high and it changes. so I need something dynamic and generic.
tharkun
A: 

I would suggest looking at information_schema. The following code is untested but should theoretically work. Obviously replace your table name with an appropriate table name or link to information_schema.tables and use the table_type in your where clause

select concat('column', column_name) from information_schema.columns where table_name ='your table name'
Steve
why should it work for selection in the information_schema if it doesn't work otherwise?
tharkun
This fetches the column names, not the contents of the column (which name is dynamic).
Arjan
yes, makes sense, thanks for the hint! I'm already trying this now...
tharkun
Ehhh, but you want to select the columns VALUES, right? Not the NAMES? I think the above will not help you select from dynamic column names. Selecting the column names (without selecting) does not need any rocket science -- it's actually already done in the example you posted in your question.
Arjan
SELECT col.`COLUMN_NAME`FROM `information_schema`.`COLUMNS` AS colWHERE col.`TABLE_NAME` LIKE 'mytable'AND col.`COLUMN_NAME` = CONCAT( 'column', 35 )
tharkun
but the resulting column name is not accepted as a column name. so same problem as before.
tharkun
+1  A: 

You cannot meta-program SQL like that (unless stored procedures really are that powerful - I wouldn't know).

You should poll your column-logic out a level and generate your SQL in whatever programming language you use to invoke this query with. This, of course, is under the assumption that the query is invoked programatically in the first place.

If this is for ad hoc queries, then I would simply modify the SQL to fit every time I wanted to run the query.

But I will humbly submit that SQL simply is not designed for this kind of meta-programming.

Christian Vest Hansen
I think that's the case and I have to accept that I can't do what I want in one query, as nice as it would have been.
tharkun