table 中的 SELECT 行在一秒钟内没有相关条目 table

SELECT rows from a table that don't have related entries in a second table

那是我在 SQL 使用 PostgreSQL 9.4 的第一天,我对一些事情感到困惑。我认为我已经接近但还不够:

Table定义:

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)
  );

我需要如何将这两个查询合并为一个:

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';

我试过类似的方法,但没有用。我需要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;

有人能帮忙吗?

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;
  • 如评论所述,您需要一个连接条件。就诊与医生有何关联?通常,您会有一个 visit.doctor_id 引用 doctor.doctor_id.

  • LEFT JOIN / IS NULL排除在上述期间就诊的医生。这是几种可能的技术之一:

    • Select rows which are not present in other table
  • 日期必须大于下限AND小于上限。 OR这里是错误的。

  • 最好使用 ISO 8601 日期格式,无论您的语言环境如何,它都是明确的。

    • field value between date range

LEFT JOIN ... WHERE NULL 构造的替代方法是普通的 WHERE NOT EXISTS(...) 反连接。 [完全等同于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;

你快到了... 而不是

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;

尝试

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;

以防万一 - table/column 在 Postgres 中默认名称不区分大小写。如果您希望它们区分大小写 - 您必须将它们写在 "" 中。

我终于找到了解决方案,这与您的解决方案非常相似,我post在这里帮助其他有类似问题的人

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;

谢谢大家的帮助!