我正在为飞行学校创建一个预订数据库,其中包含时间表和飞行员作为当天航班的视觉日程表类型表示。我让它工作正常,直到他们获得了一架有时需要副驾驶的飞机。
他们有一些永久在名单上的飞行员(他们想一直在计划器上看到),还有一些偶尔使用(他们只想在当天为他们预订时在那里)。他们还希望能够在计划器上设置自己的飞行员顺序,所以我int_LISTORDER
在员工记录中设置了一个字段,在列表中设置了顺序,并且int_LISTORDER
只有在预订时才会出现 500 及以上的任何人为他们。为了在组合中添加一名副驾驶,txt_STAFFNAME2
除了机长之外,预订还添加了一个字段txt_STAFFNAME
。
我使用原始的仅飞行员版本进行了以下查询,以选择在任何特定日期($tl_date
变量)进行预订的所有飞行员,这些飞行员与工作人员列表中的飞行员列表匹配并获取他们的列表顺序,并将其与列表顺序低于 500 的飞行员列表:
(SELECT DISTINCT tbl_booking_details.txt_STAFFNAME AS txt_PILOT,
tbl_staff_details.int_LISTORDER AS int_ORDER
FROM tbl_booking_details
INNER JOIN tbl_staff_details
ON
tbl_staff_details.txt_PILOTNAME = tbl_booking_details.txt_STAFFNAME
WHERE tbl_booking_details.date_DATE = '{$tl_date}'
AND tbl_staff_details.txt_PILOTNAME !='--none--'
AND tbl_booking_details.txt_COMPLETECODE !='CANCELLED')
UNION
(SELECT tbl_staff_details.txt_PILOTNAME AS txt_PILOT,
tbl_staff_details.int_LISTORDER AS int_ORDER
FROM `tbl_staff_details`
WHERE tbl_staff_details.bool_ISPILOT =1
AND tbl_staff_details.bool_CURRENT =1
AND tbl_staff_details.txt_PILOTNAME != '--none--'
AND tbl_staff_details.int_LISTORDER <500)
ORDER BY ".STAFF_LIST_ORDER;
这个查询扩展了我对 MySQL 的了解,所以我需要弄清楚如何在语句之前修改查询的第一部分,UNION
以获取'tbl_booking_detail'中的飞行员 ( txt_STAFFNAME
) 和副驾驶 ( ) 列表及其列表-将在该特定日期有预订的“tbl_staff_detail”txt_STAFFNAME2
订购(int_LISTORDER
)作为“txt_PILOT”和“int_ORDER”放入一张桌子。希望 WHERE 子句中的字段和值的名称是自解释的。
任何人都可以协助修改此查询以满足新标准吗?
问候
布雷登。
编辑:@bonCodigo
表详细信息是(减去许多不相关的字段):
tbl_booking_details
------------------------------------------------------------------------------
ID | txt_STAFFNAME | txt_STAFFNAME2 | date_DATE | txt_COMPLETECODE | ...
------------------------------------------------------------------------------
475 | Joe A | Joe B | 2013-01-14 | CONFIRMED | ...
------------------------------------------------------------------------------
476 | Joe A | --none-- | 2013-01-14 | CONFIRMED | ...
------------------------------------------------------------------------------
476 | Joe C | Joe A | 2013-01-14 | CANCELLED | ...
------------------------------------------------------------------------------
477 | Joe B | Jane D | 2013-01-14 | CONFIRMED | ...
------------------------------------------------------------------------------
478 | Joe E | Jane D | 2013-01-14 | CONFIRMED | ...
------------------------------------------------------------------------------
tbl_staff_details
----------------------------------------------------------------------------
ID | txt_PILOTNAME | bool_CURRENT| bool_ISPILOT | int_LISTORDER | ...
----------------------------------------------------------------------------
1 | --none-- | 1 | 1 | 0 | ...
----------------------------------------------------------------------------
2 | Joe A | 1 | 1 | 5 | ...
----------------------------------------------------------------------------
4 | Joe B | 1 | 1 | 15 | ...
----------------------------------------------------------------------------
5 | Joe C | 1 | 1 | 510 | ...
----------------------------------------------------------------------------
6 | Jane D | 1 | 1 | 20 | ...
----------------------------------------------------------------------------
7 | Joe E | 1 | 1 | 520 | ...
----------------------------------------------------------------------------
期望的结果:
--------------------------
| txt_PILOT | int_ORDER |
--------------------------
| Joe A | 5 |
--------------------------
| Joe B | 15 |
--------------------------
| Jane D | 20 |
--------------------------
| Joe E | 520 |
--------------------------