我需要帮助编写查询以获取一些信息,但我无法编写它。
[table_People]
int id
var name
[table_Tools]
int id
var name
[table_Activity1]
int person_id
int tool_id
date delivery_date
[table_Activity2]
int person_id
int tool_id
date installation_date
该查询需要返回所有人员的列表以及他们在活动 1 或 2 中使用的最新工具的名称(两者之间发生的最新活动)。
SELECT
people.id AS personId,
people.name AS personName,
(
SELECT
tools.name AS toolName
FROM
activity1
JOIN
tools ON tools.id=activity1.tool_id
WHERE
activity1.id=people.id
UNION ALL
SELECT
tools.name AS toolName
FROM
activity2
JOIN
tools ON tools.id=activity2.tool_id
WHERE
activity2.id=people.id
ORDER BY
installationDate,deliveryDate
) AS toolName
FROM
people
ORDER BY
people.name
ASC
我遇到的问题是我无法按日期(交付或安装)排序,因为我收到错误,因为它们是不同的列名。