5

I want to use a registry as a column name but the registry is variable and I don't know when it changes.

Example:

Config (field) = 'Medicine'
FieldContent (another field) = 'Remedy name'

A want to make this:

Medicine (use content of Config as column name) = 'Remedy Name' (as registry)

What have I tried?

SET @CONFIG = SELECT CONFIG;

SELECT FIELDCONTENT AS @CONFIG FROM TABLENAME;

MySql says that I can't use a variable as column name. There's other way?

actual Config Content Medicine RemedyName

Wanted Medicine
RemedyName

Thanks!

1 Answer 1

9

My idea is to use a prepared statement:

SET @config := (SELECT CONFIG FROM yourtable WHERE id=1);
SET @sql := CONCAT('SELECT FIELDCONTENT AS `', @config, '` FROM TABLENAME');

PREPARE stmt FROM @sql;
EXECUTE stmt;
Sign up to request clarification or add additional context in comments.

Comments

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.