【问题标题】:Using the value of a field as column name in a query在查询中使用字段的值作为列名
【发布时间】:2010-06-22 14:00:48
【问题描述】:

我试图找出一种方法来仅获取与单元所在的操作步骤相关的特定字段。我有一个包含单元及其操作步骤的表,然后我有另一个表,其中有一列用于每一步。我只想拉出与该单元当前步骤相关的列。

这是我的尝试,但我似乎无法找到一种方法来使用 operation 的值作为参考。我的问题是我需要匹配子查询中 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

【问题讨论】:

    标签: mysql pass-by-reference


    【解决方案1】:

    我相信您的问题在于表格的设计,而不是您的查询。

    如果我是正确的,你的表格看起来像这样:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多