[英]SELECT rows from a table that don't have related entries in a second table
That's my first day in SQL using PostgreSQL 9.4 and I'm lost with some things. 那是我使用PostgreSQL 9.4进行SQL的第一天,我迷失了一些东西。 I think that I'm close but not enough:
我认为我已经接近但还不够:
Table definition: 表定义:
CREATE TABLE DOCTOR (
Doc_Number INTEGER,
Name VARCHAR(50) NOT NULL,
Specialty VARCHAR(50) NOT NULL,
Address VARCHAR(50) NOT NULL,
City VARCHAR(30) NOT NULL,
Phone VARCHAR(10) NOT NULL,
Salary DECIMAL(8,2) NOT NULL,
DNI VARCHAR(10) UNIQUE
CONSTRAINT pk_Doctor PRIMARY KEY (Doc_Number)
);
CREATE TABLE VISIT (
Doc_Number INTEGER,
Pat_Number INTEGER,
Visit_Date DATE,
Price DECIMAL(7,2),
CONSTRAINT Visit_pk PRIMARY KEY (Doc_Number, Pat_Number, Visit_Date),
CONSTRAINT Visit_Doctor_fk FOREIGN KEY (Doc_Number) REFERENCES DOCTOR(Doc_Number),
CONSTRAINT Visit_PATIENT_fk FOREIGN KEY (Pat_Number) REFERENCES PATIENT(Pat_Number)
);
I need how to join these two queries into one: 我需要如何将这两个查询合并为一个:
SELECT d.City, d.Name
FROM DOCTOR d, VISIT v
WHERE d.Specialty = 'family and comunity'
ORDER BY d.Name;
SELECT * FROM VISIT
WHERE DATE (Visit_Date)<'01/01/2012'
OR DATE(Visit_Date)>'31/12/2013';
I tried something like this but it doesn't work. 我尝试了类似的方法,但是没有用。 I need the doctors of that specialty that didn't do any visit in 2012 and 2013.
我需要该专业的医生,他们在2012年和2013年都没有进行过任何访问。
SELECT City, Name
FROM DOCTOR d
WHERE d.Specialty = 'family and comunity'
AND NOT IN(SELECT *
FROM VISIT
WHERE Visit_Date BETWEEN '2012-01-01' and '2013-12-31')
ORDER BY d.Name;
Can anyone help? 有人可以帮忙吗?
SELECT d.name, d.city
FROM doctor d
LEFT JOIN visit v ON v.doc_number = d.doc_number
AND v.visit_date BETWEEN '2012-01-01' AND '2013-12-31'
WHERE d.specialty = 'family and community' -- or 'family and comunity'?
AND v.doc_number IS NULL
ORDER BY d.name;
As commented you need a join condition. 如前所述,您需要加入条件。 How are visits connected to doctors?
拜访如何与医生联系起来? Typically, you would have a
visit.doctor_id
referencing doctor.doctor_id
. 通常,您将具有一个
visit.doctor_id
doctor.doctor_id
。
Using LEFT JOIN / IS NULL
to rule out doctors who have visits in said period. 使用
LEFT JOIN / IS NULL
排除在此期间曾去过诊的医生。 This is one of several possible techniques: 这是几种可能的技术之一:
Dates must be greater than the lower bound AND
smaller than the upper bound. 日期必须比下限更大
AND
比上限小。
would be wrong here. OR
在这里是错误的。 OR
It's better to use ISO 8601 date format which is unambiguous regardless of your locale. 最好使用明确的ISO 8601日期格式,而不管您的区域设置如何。
Alternative to the LEFT JOIN ... WHERE NULL
construct is the plain WHERE NOT EXISTS(...)
anti-join. LEFT JOIN ... WHERE NULL
构造的替代方法是普通的WHERE NOT EXISTS(...)
反LEFT JOIN ... WHERE NULL
。 [It is completely equivalent to erwin's query] [完全等同于erwin的查询]
SELECT d.name, d.city
FROM doctor d
WHERE d.specialty = 'family and community'
AND NOT EXISTS (
SELECT 13
FROM visit v WHERE v.doc_number = d.doc_number
AND v.visit_date BETWEEN '2012-01-01' AND '2013-12-31'
)
ORDER BY d.name;
You were almost there... Instead of 你快到了...而不是
SELECT City, Name
FROM DOCTOR d
WHERE d.Specialty = 'family and comunity'
AND NOT IN(SELECT *
FROM VISIT
WHERE Visit_Date BETWEEN '2012-01-01' and '2013-12-31')
ORDER BY d.Name;
try 尝试
SELECT City, Name
FROM DOCTOR d
WHERE d.Specialty = 'family and comunity'
AND doc_number NOT IN(SELECT doc_number -- or SELECT DISTINCT doc number - to get fewer rows from the subquery
FROM VISIT
WHERE Visit_Date BETWEEN '2012-01-01' and '2013-12-31')
ORDER BY d.Name;
Just in case - table/column names are case-insensitive by default in Postgres. 以防万一-在Postgres中,表/列名默认不区分大小写。 If you want them case sensitive - you have to write them in "" .
如果您希望它们区分大小写-您必须将它们写在“”中。
I finally found the solution, this is quite similar to your solutions, I post here to help another people with similar problems 我终于找到了解决方案,这与您的解决方案非常相似,我在这里发布以帮助遇到类似问题的其他人
SELECT City, Name
FROM DOCTOR d, VISIT v
WHERE d.Specialty = 'family and comunity'
AND not exists(SELECT *
FROM visit v WHERE v.doc_number = d.doc_number
AND v.visit_date BETWEEN '2012-01-01' AND '2013-12-31')
GROUP BY name, city
ORDER BY d.Name;
Thank you all for your help! 谢谢大家的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.