简体   繁体   English

不带where子句的联接查询

[英]Join query without where clause

The two queries below output the same result. 下面的两个查询输出相同的结果。 I understand the first one, but can someone explain how the 2nd query works the same. 我理解第一个查询,但是有人可以解释第二个查询的工作原理如何。

SELECT p0_.name AS name_0
FROM patient p0_ INNER JOIN consultant c1_
ON c1_.id = p0_.consultant_id
WHERE c1_.id=1; 

SELECT p0_.name AS name_0
FROM patient p0_ INNER JOIN consultant c1_
ON (c1_.id = 1)

These two queries give the same result only by coincidence, just in a case when there is ony 1 row in patients table. 仅在patients表中只有1行的情况下,这两个查询只能通过巧合得出相同的结果。
These queries are different, and give different results. 这些查询是不同的,并且给出不同的结果。
Please examine this simple demo: http://sqlfiddle.com/#!9/1987a/1 请检查这个简单的演示: http : //sqlfiddle.com/#!9/1987a/1

CREATE TABLE patient (
  patient_id int,
  name varchar(20),
  consultant_id int
);

insert into patient values(1,'Patient 1', 1); 
insert into patient values(2,'Patient 2', 2); 

CREATE TABLE consultant(
  id int
);

insert into consultant values(1),(2);

SELECT p0_.name AS name_0
FROM patient p0_ INNER JOIN consultant c1_
ON c1_.id = p0_.consultant_id
WHERE c1_.id=1; 

|    name_0 |
|-----------|
| Patient 1 |

SELECT p0_.name AS name_0
FROM patient p0_ INNER JOIN consultant c1_
ON (c1_.id = 1)

|    name_0 |
|-----------|
| Patient 1 |
| Patient 2 |

The main reason for primary and foreign keys is to enforce data consistency. 主键和外键的主要原因是强制数据一致性。

A primary key enforces the consistency of uniqueness of values over one or more columns. 主键可在一个或多个列上强制值唯一性的一致性。 If an ID column has a primary key then it is impossible to have two rows with the same ID value. 如果ID列具有主键,则不可能有两行具有相同的ID值。 Without that primary key, many rows could have the same ID value and you wouldn't be able to distinguish between them based on the ID value alone. 没有该主键,许多行可能具有相同的ID值,您将无法仅根据ID值来区分它们。

A foreign key enforces the consistency of data that points elsewhere. 外键可增强指向其他位置的数据的一致性。 It ensures that the data which is pointed to actually exists. 它确保所指向的数据确实存在。 In a typical parent-child relationship, a foreign key ensures that every child always points at a parent and that the parent actually exists. 在典型的父子关系中,外键可确保每个孩子始终指向父母,并且父母实际存在。 Without the foreign key you could have "orphaned" children that point at a parent that doesn't exist. 没有外键,您可能会有“孤立的”孩子指向一个不存在的父母。

You will not see the inconsistency of data you would have since you only have one record and retrieved just one column. 您将不会看到数据不一致的情况,因为您只有一条记录并且仅检索到一列。

Let's say we have these tables and columns, and records. 假设我们有这些表和列以及记录。

CREATE TABLE patient (
   patient_id INT,
   name VARCHAR(20),
   consultant_id INT
);

INSERT INTO patient VALUES(100,'Patient 1', 1); 
INSERT INTO patient VALUES(200,'Patient 2', 1); 
INSERT INTO patient VALUES(300,'Patient 3', 1); 
INSERT INTO patient VALUES(400,'Patient 4', 2); 
INSERT INTO patient VALUES(500,'Patient 5', 2); 
INSERT INTO patient VALUES(600,'Patient 6', 3); 



CREATE TABLE consultant(
   id INT,
   name VARCHAR(20)
);

INSERT INTO consultant VALUES(1,'Consultant 1');
INSERT INTO consultant VALUES(2,'Consultant 2');
INSERT INTO consultant VALUES(3,'Consultant 3');

See that we have name as well for consultants and we wanted to see which consultant attended the patient 看到我们也有顾问的名字,我们想看看哪个顾问参加了病人

SELECT p0_.name AS patientname_0,
       c1_.name AS consultantname_0
  FROM patient p0_ 
 INNER JOIN consultant c1_
    ON c1_.id = p0_.consultant_id
 WHERE (c1_.id = 1);

Result 结果

patientname_0   consultantname_0
Patient 1       Consultant 1
Patient 2       Consultant 1
Patient 3       Consultant 1

However, with the query without the keys in the ON clause 但是,对于在ON子句中没有键的查询

SELECT p0_.name AS patientname_0,
       c1_.name AS consultantname_0
  FROM patient p0_ 
 INNER JOIN consultant c1_
    ON (c1_.id = 1);

resulted in inconsistent data for consultantname of the rest of the patients whose consultant is not 1 导致其他顾问不是1的患者的顾问名数据不一致

patientname_0   consultantname_0
Patient 1       Consultant 1
Patient 2       Consultant 1
Patient 3       Consultant 1
Patient 4       Consultant 1
Patient 5       Consultant 1
Patient 6       Consultant 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM