这是我在两张桌子上的东西。
TABLE tbl_appuntamento => 此表包含约会列表
+------+----------------------+-----------+---------+
| id | data | slot | id_sede |
+------+----------------------+-----------+---------+
| 1 | 2017-03-27 | 10:00:00 | 1 |
| 2 | 2017-03-27 | 10:00:00 | 1 |
| 3 | 2017-03-28 | 11:00:00 | 1 |
| 4 | 2017-03-28 | 12:00:00 | 1 |
+------+----------------------+-----------+---------+
TABLE tbl_blocco_operativo => 此表包含无法设置约会的日期或时段列表
+------+----------------------+-----------+---------+------------+
| id | data | slot | id_sede | is_fullday |
+------+----------------------+-----------+---------+------------+
| 1 | 2017-03-27 | 10:00:00 | 1 |0 |
| 2 | 2017-03-27 | 11:00:00 | 1 |0 |
| 3 | 2017-03-28 | 00:00:00 | 1 |1 |
+------+----------------------+-----------+---------+------------+
我有这个查询
SELECT appuntamento.*,blocco.slot blockSlot, blocco.is_fullday blockFullday
FROM tbl_appuntamento appuntamento
LEFT JOIN tbl_argomento argomento ON argomento.id = appuntamento.id_argomento
LEFT JOIN tbl_blocco_operativo blocco ON blocco.data = appuntamento.data AND blocco.id_sede = appuntamento.id_sede
AND ((blocco.is_fullday = 0 AND blocco.slot = appuntamento.slot)
OR (blocco.is_fullday = 1 AND blocco.slot <> appuntamento.slot))
WHERE appuntamento.id_sede = :locationId
AND appuntamento.data >= :startDate
AND appuntamento.data <= :endDate
GROUP BY appuntamento.id
ORDER BY appuntamento.data, appuntamento.slot
目标是在结果中插入第二个表中的“is_fullday”和“slot”列,以检查是否与日程表有任何冲突,因为可以在任何人在表“blocco_operativo”中创建记录之前预订约会
现在这似乎可行,但我不确定第二个 JOIN 中的“或”条件是最佳解决方案。没关系,还是我错过了什么?