我已经写了两张桌子。第一个是名为time_dimension的一年中的所有日期,第二个名为tbl_holidays的日期是今年的公共假日。但是,我想在没有周末和假期的情况下度过这一天。我写的查询就像;
SELECT ti.id, ti.db_date, ti.year, ti.month, ti.day, ti.quarter, ti.week, ti.day_name, ti.month_name
FROM time_dimension ti
LEFT JOIN tbl_holidays tb on ti.db_date=tb.startdate
where day_name in ('Monday','Tuesday','Wednesday','Thursday','Friday');
任何人都可以帮我摆脱这个查询中的假期表吗?
更新答案:
SELECT ti.id, ti.db_date, ti.year, ti.month, ti.day, ti.quarter, ti.week, ti.day_name, ti.month_name
FROM DeJuMa.time_dimension ti
LEFT JOIN tbl_holidays tb on ti.db_date=tb.startdate
where day_name in ('Monday','Tuesday','Wednesday','Thursday','Friday')
AND ti.db_date NOT IN (select startdate from tbl_holidays tb );
答案 0 :(得分:2)
SELECT ti.id, ti.db_date, ti.year, ti.month, ti.day, ti.quarter, ti.week, ti.day_name, ti.month_name
FROM time_dimension ti WHERE day_name NOT IN ('Saturday','Sunday')
AND ti.db_date NOT IN (SELECT startdate FROM tbl_holidays);
答案 1 :(得分:1)
您可以使用NOT EXISTS
排除假期:
SELECT *
FROM time_dimension ti
WHERE
NOT EXISTS(SELECT 1 FROM tbl_holidays WHERE startdate = ti.db_date)
AND day_name IN ('Monday','Tuesday','Wednesday','Thursday','Friday');