我試圖確定誰尚未登記在電話上的所有星期六在2017年7月1日和12月31日MySQL的雙左聯接預期結果的返回相反
之間在MySQL中,我有兩個表和1觀點: j3_aqi_userdata_v(圖) j3_sv_apptpro3_requests(表) j3_oncall_calendar_dates(表)
我需要讓所有的人都從j3_aqi_userdata_v誰不j3_sv_apptpro3_requests顯示的列表。簡單的左連接應該這樣做,但不是那麼多。爲什麼,你可能會問?那麼,我還需要在where子句中添加一些語句來排除不是星期六,幾年不是今年,以及一月到六月的日子。
我有以下,但它只有返回誰已預訂隨叫隨到的人:
select
*
from
j3_aqi_userdata_v t1
left join (j3_sv_apptpro3_requests t2)
on (t1.id=t2.user_id)
left join j3_oncall_calendar_dates t3
on (t2.startdate=t3.db_date)
where
t3.saturday_flag = 'T'
and
t3.year=year(current_date)
and
t3.month >= 7
任何人都知道我做錯了嗎?
表結構:
j3_aqi_userdata_v
# Name Type Collation Attributes Null Default Comments Extra
1 id int(11) No 0
2 author varchar(400) utf8mb4_general_ci No
3 username varchar(150) utf8mb4_general_ci No
4 email varchar(100) utf8mb4_general_ci No
5 employeeid bigint(21) No 0
6 coach longtext utf8mb4_general_ci Yes NULL
7 geo longtext utf8mb4_general_ci Yes NULL
8 team longtext utf8mb4_general_ci Yes NULL
9 manager longtext utf8mb4_general_ci Yes NULL
10 country longtext utf8mb4_general_ci Yes NULL
j3_sv_apptpro3_requests
CREATE TABLE `j3_sv_apptpro3_requests` (
`id_requests` int(11) NOT NULL,
`user_id` int(11) DEFAULT NULL,
`operator_id` int(11) DEFAULT NULL,
`name` varchar(255) NOT NULL DEFAULT '',
`phone` varchar(20) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`resource` varchar(50) NOT NULL DEFAULT '',
`category` varchar(50) NOT NULL DEFAULT '',
`startdate` date DEFAULT NULL,
`starttime` time DEFAULT NULL,
`enddate` date DEFAULT NULL,
`endtime` time DEFAULT NULL,
`comment` varchar(255) DEFAULT NULL,
`admin_comment` varchar(255) DEFAULT NULL,
`request_status` varchar(20) DEFAULT 'new',
`payment_status` varchar(20) DEFAULT 'pending',
`show_on_calendar` char(3) NOT NULL DEFAULT '1',
`calendar_comment` varchar(200) NOT NULL DEFAULT '',
`calendar_category` int(11) DEFAULT NULL,
`calendar_calendar` int(11) DEFAULT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`cancellation_id` varchar(255) DEFAULT NULL,
`service` varchar(50) DEFAULT NULL,
`txnid` varchar(255) DEFAULT NULL,
`sms_reminders` varchar(10) DEFAULT 'No',
`sms_phone` varchar(50) DEFAULT NULL,
`sms_dial_code` varchar(4) DEFAULT '1',
`google_event_id` varchar(255) DEFAULT '',
`google_calendar_id` varchar(255) DEFAULT '',
`booking_total` decimal(10,2) DEFAULT '0.00',
`booking_deposit` decimal(10,2) DEFAULT '0.00',
`booking_due` decimal(10,2) DEFAULT '0.00',
`coupon_code` varchar(255) DEFAULT NULL,
`booked_seats` int(11) DEFAULT '1',
`booking_language` varchar(25) DEFAULT 'en-gb',
`credit_used` float(10,2) DEFAULT '0.00',
`payment_processor_used` varchar(30) DEFAULT 'None',
`manual_payment_collected` float(10,2) DEFAULT '0.00',
`last_change_operator` int(11) DEFAULT NULL,
`invoice_number` varchar(255) DEFAULT '',
`gift_cert` varchar(255) DEFAULT '',
`cancel_reason` varchar(255) DEFAULT '',
`loyalty_points` int(11) DEFAULT '0',
`checked_out` smallint(5) DEFAULT '0',
`checked_out_time` datetime DEFAULT NULL,
`ordering` smallint(5) DEFAULT '0',
`published` tinyint(3) UNSIGNED DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
ALTER TABLE `j3_sv_apptpro3_requests`
ADD PRIMARY KEY (`id_requests`),
ADD KEY `startdate` (`startdate`);
ALTER TABLE `j3_sv_apptpro3_requests`
MODIFY `id_requests` int(11) NOT NULL AUTO_INCREMENT;COMMIT;
j3_oncall_calendar_dates
CREATE TABLE `j3_oncall_calendar_dates` (
`id` int(11) NOT NULL,
`db_date` date NOT NULL,
`year` int(11) NOT NULL,
`month` int(11) NOT NULL,
`day` int(11) NOT NULL,
`quarter` int(11) NOT NULL,
`week` int(11) NOT NULL,
`day_name` varchar(9) NOT NULL,
`dayofweek` int(11) NOT NULL,
`month_name` varchar(9) NOT NULL,
`holiday_flag` char(1) DEFAULT 'F',
`saturday_flag` char(1) DEFAULT 'T'
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
ALTER TABLE `j3_oncall_calendar_dates`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `td_ymd_idx` (`year`,`month`,`day`),
ADD UNIQUE KEY `td_dbdate_idx` (`db_date`);
COMMIT;
的聲音,我LEFT JOIN轉換爲內部連接,因爲關閉WHERE語句移動WHERE過濾器進入ON子句。 'ON(t2.startdate = t3.db_date)AND t3.saturday_flag ='T'....' –