[英]select all from table1 with zero entries in table2 in sqlite?
Consider 2 tables, user and contact: 考虑2个表,用户和联系人:
CREATE TABLE user (
id INT,
name VARCHAR(36)
);
CREATE TABLE contact (
id INT,
phone INT,
userID INT
);
INSERT INTO user (id,name) VALUES
(1,'Frank'),
(2,'Henry'),
(3,'John')
INSERT INTO contact (id,phone,userID) VALUES
(1,911,1),
(2,922,2),
(3,933,2)
I am interested in all user entries that do not have any contact. 我对没有任何联系的所有用户条目感兴趣。
The outer join of these tables returns 4 results: 这些表的外部联接返回4个结果:
SELECT contact.*, user.*
FROM contact
LEFT JOIN user
ON contact.userID = user.id
UNION ALL
SELECT contact.*, user.*
FROM user
LEFT JOIN contact
ON contact.userID = user.id
WHERE contact.userID IS NULL
How do I select all user where contact.userID is null (1 result, in this example)? 如何选择contact.userID为null(在此示例中为1个结果)的所有用户?
This is your second subquery: 这是您的第二个子查询:
SELECT c.*, u.*
FROM user u LEFT JOIN
contact c
ON c.userID = u.id
WHERE c.userID IS NULL;
It does exactly what you want, although I would remove the c.*
from the SELECT
. 它确实可以满足您的要求,尽管我会从SELECT
删除c.*
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.