我有两个表,一个:
INSERT INTO `fictional_companies` (`ID`, `name`)
VALUES
(8209948, 'Moulon Rouge LTD'),
(11399177, 'Burgers Burgers Burgers LTD');
和两个:
INSERT INTO `processed` (`ID`)
VALUES
('8209948');
我想从fictional_companies
表中返回结果,但前提是processed
表中不存在结果。
在示例情况下,将是结果:
(11399177, 'Burgers Burgers Burgers LTD')
答案 0 :(得分:3)
LEFT JOIN
基于与右侧表上的IS NULL
检查的适当关系,将获得与右侧表不匹配的那些行
SELECT fc.*
FROM fictional_companies AS fc
LEFT JOIN processed AS p ON p.ID = fc.ID
WHERE p.ID IS NULL
使用Correlated Subqueries with Not Exists
可能是另一种方法SELECT fc.*
FROM fictional_companies AS fc
WHERE NOT EXISTS (SELECT 1 FROM processed AS p
WHERE p.ID = fc.ID)
答案 1 :(得分:2)
使用左连接并检查右表中的空值仅在其中选择那些值
Attendance::where('attendance_date' , Carbon::now()->format('Y-m-d'))
->where('user_id', $this->id)
->where('scan_in_location', request()->scan_in_location)
->whereNotNull('scan_in_time')
->whereBetween('created_at' , [Carbon::now()->subMinutes(5), Carbon::now()])
->exists();
答案 2 :(得分:1)
使用NOT IN
:
SELECT * FROM `fictional_companies` WHERE `ID` NOT IN (SELECT `ID` FROM `processed`)
我相信代码很容易理解。