在查询中使用字段的值作为列名称

在查询中使用字段的值作为列名称

问题描述:

我试图找出一种方法来只抓住与单元所在操作步骤有关的特定字段。我有一个带有单位及其操作的表一步,然后我有另一个表,每个步骤都有一列。我只想拉取与该单位当前步骤相关的列。在查询中使用字段的值作为列名称

这是我的尝试,但我似乎无法找到一种方法来使用操作的值作为参考。我的问题是我需要匹配子查询中t1.operation的值而不是t1.operation的值的字段。这有什么意义吗?

SELECT t1.*, 
    (SELECT t1.operation 
    FROM report_tables.roc_capacity_standards As t2 
    WHERE t1.assembly=t2.part_number) As standard 
FROM report_tables.resource_transactions As t1 
WHERE t1.date BETWEEN '2010-06-01' 
        AND '2010-06-19' 
GROUP BY job, employee 
ORDER BY operation; 

网上搜索后,我发现有一个人说,这是不可能的,主要因为MySQL不知道它是什么,以优化其基于索引等这是属于动态表名,虽然让我查询不知道它是一样的。

这是在这里找到:Reference

我相信你的问题就出在你的表,而不是查询的设计。

如果我是正确的,你的表类似于此:

units (unit_id, current_operation, date, ...) 
operations (unit_id, operation_1_value, operation_2_value, operation_3_value, ...) 

这是设计不良,因为它会导致像现在有你的问题。

一个更好的设计是

units (unit_id, *other unit specific info* ...) 
operations (operation_id, operation_name, *other general operation info*) 
unit_operation (unit_id, operation_id, *operation_results, *operation_values, *date_started, *date_ended)