我有一个包含 4 个子查询的查询。查询是这样的:
SELECT
(SELECT
COUNT(id)
FROM timelog
WHERE emp_id = 1
AND am_in > GET_TIME_IN1(emp_id, DATE)) AS tardy1,
(SELECT
COUNT(id)
FROM timelog
WHERE emp_id = 1
AND pm_in > GET_TIME_IN2(emp_id, DATE)) AS tardy2,
(SELECT balance FROM leave_credit lc JOIN leave_type lt ON lc.leave_type_id = lt.id WHERE emp_id = 1 AND lt.active = TRUE) AS balance,
(SELECT leave_type_id FROM leave_credit lc JOIN leave_type lt ON lc.leave_type_id = lt.id WHERE emp_id = 1 AND lt.active = TRUE) AS leave_type_id
我这样做是为了让我只有 1 个从 PHP 到 SQL 服务器的查询字符串,并在一个实例中获取所有结果。我知道子查询会影响性能,但在我的情况下,有没有更好的方法来解决我的问题?
示例数据:时间日志表
离开信用表