SQL 查询中的 if 条件
if conditions in an SQL query
我有以下SQL查询
SELECT l.LoginName ,p.name as project_name, p.start_date as
project_start_date, p.end_date as project_end_date,
pe.budget, pe.start_date, pe.end_date
FROM project_expenses_new pe
INNER JOIN projects p ON pe.project_uuid=p.uuid
INNER JOIN LoginUsers l ON pe.employee_uuid=l.uuid
WHERE pe.employee_uuid='8a373382-c921-11ec-a7c7-005056020962' AND
('2021-08-01' BETWEEN p.start_date AND p.end_date);
基本上我使用了 3 个 table,但其中只有 2 个 table 与查询相关。这些 table 是项目,projects_expenses_new。两个 table 都有字段 start_date 和 end_date.
我想做的是检查 project_expenses_new table 中 start_date 和 end_date 的值是否具有非空值。在这种情况下,我将检查我要查找的日期是否在 project_expenses_new table 的 start_date 和 end_date 之间。因此,如果 pe.start_date 和 pe.end_date 的值不为空,则查询将为:
SELECT l.LoginName ,p.name as project_name, p.start_date as
project_start_date, p.end_date as
project_end_date, pe.budget, pe.start_date, pe.end_date
FROM project_expenses_new pe
INNER JOIN projects p ON pe.project_uuid=p.uuid
INNER JOIN LoginUsers l ON pe.employee_uuid=l.uuid
WHERE pe.employee_uuid='8a373382-c921-11ec-a7c7-005056020962' AND
('2021-08-01' BETWEEN pe.start_date AND pe.end_date);
另一方面,如果project_expenses_new table上的start_date和end_date的值为null,我想查询条件获取记录在项目 table start_date 和 end_date 之间。这些值永远不会为 NULL。
我不确定如何在查询中加入 if 条件。
如果另一个日期是NULL
,您可以使用IFNULL()
来使用一个日期。
WHERE pe.employee_uuid = '8a373382-c921-11ec-a7c7-005056020962'
AND '2021-08-01' BETWEEN IFNULL(pe.start_date, p.start_date) AND
IFNULL(pe.end_date, p.end_date)
如果您必须检查 start_date
和 end_date
是否都为空:
SELECT l.LoginName ,p.name as project_name, p.start_date as
project_start_date, p.end_date as project_end_date,
pe.budget, pe.start_date, pe.end_date
FROM project_expenses_new pe
INNER JOIN projects p ON pe.project_uuid=p.uuid
INNER JOIN LoginUsers l ON pe.employee_uuid=l.uuid
WHERE pe.employee_uuid='8a373382-c921-11ec-a7c7-005056020962' AND
'2021-08-01' BETWEEN (case when p.start_date is null or p.end_date is null then
pe.p.start_date else p.start_date end) and (case when p.start_date is null or p.end_date is null then pe.p.end_date else p.end_dateend);
我有以下SQL查询
SELECT l.LoginName ,p.name as project_name, p.start_date as
project_start_date, p.end_date as project_end_date,
pe.budget, pe.start_date, pe.end_date
FROM project_expenses_new pe
INNER JOIN projects p ON pe.project_uuid=p.uuid
INNER JOIN LoginUsers l ON pe.employee_uuid=l.uuid
WHERE pe.employee_uuid='8a373382-c921-11ec-a7c7-005056020962' AND
('2021-08-01' BETWEEN p.start_date AND p.end_date);
基本上我使用了 3 个 table,但其中只有 2 个 table 与查询相关。这些 table 是项目,projects_expenses_new。两个 table 都有字段 start_date 和 end_date.
我想做的是检查 project_expenses_new table 中 start_date 和 end_date 的值是否具有非空值。在这种情况下,我将检查我要查找的日期是否在 project_expenses_new table 的 start_date 和 end_date 之间。因此,如果 pe.start_date 和 pe.end_date 的值不为空,则查询将为:
SELECT l.LoginName ,p.name as project_name, p.start_date as
project_start_date, p.end_date as
project_end_date, pe.budget, pe.start_date, pe.end_date
FROM project_expenses_new pe
INNER JOIN projects p ON pe.project_uuid=p.uuid
INNER JOIN LoginUsers l ON pe.employee_uuid=l.uuid
WHERE pe.employee_uuid='8a373382-c921-11ec-a7c7-005056020962' AND
('2021-08-01' BETWEEN pe.start_date AND pe.end_date);
另一方面,如果project_expenses_new table上的start_date和end_date的值为null,我想查询条件获取记录在项目 table start_date 和 end_date 之间。这些值永远不会为 NULL。
我不确定如何在查询中加入 if 条件。
如果另一个日期是NULL
,您可以使用IFNULL()
来使用一个日期。
WHERE pe.employee_uuid = '8a373382-c921-11ec-a7c7-005056020962'
AND '2021-08-01' BETWEEN IFNULL(pe.start_date, p.start_date) AND
IFNULL(pe.end_date, p.end_date)
如果您必须检查 start_date
和 end_date
是否都为空:
SELECT l.LoginName ,p.name as project_name, p.start_date as
project_start_date, p.end_date as project_end_date,
pe.budget, pe.start_date, pe.end_date
FROM project_expenses_new pe
INNER JOIN projects p ON pe.project_uuid=p.uuid
INNER JOIN LoginUsers l ON pe.employee_uuid=l.uuid
WHERE pe.employee_uuid='8a373382-c921-11ec-a7c7-005056020962' AND
'2021-08-01' BETWEEN (case when p.start_date is null or p.end_date is null then
pe.p.start_date else p.start_date end) and (case when p.start_date is null or p.end_date is null then pe.p.end_date else p.end_dateend);