简体   繁体   English

从表中选择第二个表中没有相关条目的行

[英]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比上限小。 OR would be wrong here. 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.

相关问题 如果在第二个表中也没有条目,则返回行? - Return rows if they don't have entry in second table too? SQL:选择第二个表中不存在的相似行 - SQL: select similar and rows that don't exist in second table sql选择第二个表中没有关系的记录 - sql select records that don't have relation in a second table 选择在连接表中没有相应连接的行 - Select rows that don't have a corresponding join in join table 选择其他表中不存在的行,通过第二个表中的数据联接[SQL] - Select rows that don't exist in other table join by data in the second table [SQL] Rails从表中选择行,其中相关表的所有行都具有特定值 - Rails select rows from a table where a related table's all rows have a certain value 如何从表中选择在相关表中没有对应行的行? - How do I select rows from a table that do not have corresponding rows in a related table? 对两个表的SQL查询 - 返回一个表中没有另一个表的行 - SQL query on two tables - return rows in one table that don't have entries in the other 如何使用“INSERT (name, ...) INTO table SELECT name, ... FROM table_2”只插入没有相同'NAME'的行? - How to use “INSERT (name, …) INTO table SELECT name, … FROM table_2” to only insert rows who don't have the same 'NAME'? 从 SQL 表中选择行,其中第一列中的条目在第二列中具有唯一值 - Selecting rows from an SQL table where entries in the first column have unique values in the second column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM