在mysql的表中搜索列名称中的特定行值

时间:2022-09-13 10:54:22

I have a table "ABCD" which has 20 different columns by the name of my employees (This is the best possible table structure for this organisation as per requirement), Now i have a situation where need to write a SQL query to find the "Column names" for a specific row (say with id 3), which has a particular value (say c).

我有一个表“ABCD”,根据我的员工名称有20个不同的列(根据要求,这是该组织最好的表结构),现在我有一种情况需要编写SQL查询来查找“列名称“用于特定行(比如id 3),具有特定值(比如c)。

ID | Col1 | Col2 | Col3 | Col4 | Col5 | Col6
--------------------------------------------
1  | A    | B    | C    | C    | B    | A
2  | C    | B    | A    | A    | C    | B
3  | B    | A    | C    | B    | A    | C

I need to get all "column names" which has value "c" in row with id "3".

我需要获取所有“列名称”,其值为“c”,行ID为“3”。

1 个解决方案

#1


2  

you should reconsider DB design

你应该重新考虑数据库设计

    select 'col1' from abcd where id=3 and col1='C'
    union
    select 'col2' from abcd where id=3 and col2='C'
    union
    ...

It would be easy for you to generate this script dynamically in PHP

您可以轻松地在PHP中动态生成此脚本

#1


2  

you should reconsider DB design

你应该重新考虑数据库设计

    select 'col1' from abcd where id=3 and col1='C'
    union
    select 'col2' from abcd where id=3 and col2='C'
    union
    ...

It would be easy for you to generate this script dynamically in PHP

您可以轻松地在PHP中动态生成此脚本