1

我正在尝试将表连接在一起,但我无法弄清楚如何根据值将字段连接为不同的名称。例如

SELECT
     ms_workorder_events.start AS workStart // IF the value of workorder_events.eventType = 1
     ms_workorder_events.start AS qaStart // IF the value of workorder_events.eventType = 2
FROM 
  workorders
  LEFT JOIN ms_workorder_events 
  ON 
    workorders.workOrderID=ms_workorder_events.workOrderID

我已经为此工作了一段时间,但无法弄清楚。

4

2 回答 2

2

基本上你要求的是一种数据透视查询。

这里的技巧是使用 aCASE在条件匹配或不匹配时提供列值NULL,然后使用聚合函数,例如MAX()将空值折叠成单行:

  SELECT
    MAX(CASE WHEN ms_workorder_events.start = 1 THEN ms_workorder_events.start ELSE NULL END) AS workStart
    MAX(CASE WHEN ms_workorder_events.start = 2 THEN ms_workorder_events.start ELSE NULL END) AS qaStart
  FROM
    workorders
    LEFT JOIN ms_workorder_events ON workorders.workOrderID = ms_workorder_events.workOrderID

为确保返回多行,您将需要列表中的更多列SELECT和适当的GROUP BY子句。例如:

  SELECT
    /* Retrieve row per workOrderID, for example */
    workorder.workOrderID,
    MAX(CASE WHEN ms_workorder_events.start = 1 THEN ms_workorder_events.start ELSE NULL END) AS workStart
    MAX(CASE WHEN ms_workorder_events.start = 2 THEN ms_workorder_events.start ELSE NULL END) AS qaStart
  FROM
    workorders
    LEFT JOIN ms_workorder_events ON workorders.workOrderID = ms_workorder_events.workOrderID
  GROUP BY workorder.workOrderID

更新

要将上述内容合并到您的完整查询中,建议将LEFT JOIN其作为派生表。在这种情况下,子查询中不需要连接,它可以单独完成,ms_workorder_events因为它连接workorders到外部查询中。

这是一个简短的示例:

SELECT
  organizations.organizationName, 
  owner.firstName AS ownerFirstName,
  /* ... Snip all other columns... */
  workorders.title,
  workorders.workOrderId,
  /* ... Snip all other columns... */
  /* Columns from subquery join */
  eventType.workStart,
  eventType.qaStart
FROM
  workorders 
  LEFT JOIN users AS owner ON workorders.owner=owner.userID
  /* ...Snip all your other joins...*/
  LEFT JOIN (
    SELECT
      workOrderID,
      MAX(CASE WHEN ms_workorder_events.start = 1 THEN ms_workorder_events.start ELSE NULL END) AS workStart
      MAX(CASE WHEN ms_workorder_events.start = 2 THEN ms_workorder_events.start ELSE NULL END) AS qaStart
    FROM ms_workorder_events
    GROUP BY workorder.workOrderID
  ) eventType ON workorders.workOrderId = eventType.workOrderId
/* Etc... */
于 2013-05-24T13:49:02.500 回答
0

这也可以通过ms_workorder_events多次加入来完成。语法类似于:

select 
  we1.start workStart,
  we2.start as qaStart
from workorders w
LEFT JOIN ms_workorder_events we1
  ON w.workOrderID=we1.workOrderID
  and we1.eventType = 1 
LEFT JOIN ms_workorder_events we2
  ON w.workOrderID=we2.workOrderID
  and we2.eventType = 1 

所以完整的查询将是:

SELECT o.organizationName, 
  owner.firstName AS ownerFirstName, 
  owner.lastName AS ownerLastName, 
  assigned.firstName AS assignedFirstName, 
  assigned.lastName AS assignedLastName, 
  qa.firstName AS QAFirstName, 
  qa.lastName AS QALastName, 
  w.title, 
  w.type, 
  s.status, 
  w.statusDate, 
  we1.start workStart, 
  we2.start as qaStart, 
  w.targetDate
FROM workorders w
LEFT JOIN users AS owner 
  ON w.owner=owner.userID 
LEFT JOIN users AS assigned 
  ON w.assignedTo=assigned.userID 
LEFT JOIN users AS qa 
  ON w.qa=qa.userID 
LEFT JOIN organizations o
  ON w.organizationID=o.organizationID 
LEFT JOIN workordertypes wot
  ON w.typeID=wot.workOrderTypeID 
LEFT JOIN statuses s
  ON w.statusID=s.statusID 
LEFT JOIN ms_workorder_events we1
  ON w.workOrderID=we1.workOrderID 
  and we1.eventType = 1 
LEFT JOIN ms_workorder_events we2
  ON w.workOrderID=we2.workOrderID 
  and we1.eventType = 2 
WHERE w.owner ='8' 
  AND w.workOrderID > 12579
于 2013-05-24T14:30:46.487 回答