MYSQL:在单独的 table 上定义列名称的列中选择值

MYSQL: Selecting value in column where column name is defined on a seperate table

我不确定仅 SQL 是否可行...任何帮助将不胜感激:

我正在尝试检索 table B (tblProduct) 中的列中的值。 列名实际上定义在参考文献 table A (tblConfig_Group).

所以,基本上,我 给出 tblConfig_Group.groupCode 和 tblProduct.VCI

我需要在 dbColumnUK.

中设置的 tblProduct 的适当列中检索值

tblConfig_Group:

+----+-----------+-------------+
| id | groupCode | dbColumnUK  |
+----+-----------+-------------+
| 01 | A         | WindowGBP_A |
| 02 | B         | WindowGBP_B |
| 03 | C         | WindowGBP_C |
| 04 | D         | WindowGBP_D |
+----+-----------+-------------+

tbl产品:

+----+---------+-------------+-------------+-------------+-------------+
| id | VCI     | WindowGBP_A | WindowGBP_B | WindowGBP_C | WindowGBP_D |
+----+---------+-------------+-------------+-------------+-------------+
| 01 | vci1    | 1           | 11          | 21          | 31          |
| 02 | vci2    | 2           | 12          | 22          | 32          |
| 03 | vci3    | 3           | 13          | 23          | 33          |
| 04 | vci4    | 4           | 14          | 24          | 34          |
+----+---------+-------------+-------------+-------------+-------------+

您可以使用如下查询:

SELECT CASE g.dbColumnUK 
          WHEN 'WindowGBP_A' THEN p.WindowGBP_A
          WHEN 'WindowGBP_B' THEN p.WindowGBP_B
          WHEN 'WindowGBP_C' THEN p.WindowGBP_C
          WHEN 'WindowGBP_D' THEN p.WindowGBP_D
       END AS col
FROM tblProduct AS p
CROSS JOIN (SELECT dbColumnUK FROM tblConfig_Group WHERE groupCode='A') AS g 
WHERE VCI = 'vci2'

Demo here