I am struck at writing a query. Here I want to show the column name based on some specific value
For Instance, my table is like this:
id | fruits
set @q= CONCAT('SELECT columns.column_name
from table inner
join information_schema.columns
on columns.table_schema = "dbname"
and columns.table_name = "table"
and ((',
(SELECT GROUP_CONCAT(CONCAT('columns.column_name="',column_name,'"',' and table.',column_name,' = "value','"') SEPARATOR ' OR ')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table'),
'))');
prepare query from @q;
execute query;
This works for sure..
Phew!
Fiddle: http://sqlfiddle.com/#!2/9420c/2/2
PS: Replace table
with your table name ,dbname
with your db name and value
with your value
As you encrypted your question as much as possible, I can only guess.
this table smells of a bad design. And it should be like
col | value
col1 | a
col1 | b
col2 | d
and so on.
than you can easily get your col out from value