[英]SQL Simple SELECT Query
create table Person(
SSN INT,
Name VARCHAR(20),
primary key(SSN)
);
create table Car(
PlateNr INT,
Model VARCHAR(20),
primary key(PlateNr)
);
create table CarOwner(
SSN INT,
PlateNr INT,
primary key(SSN, PlateNR)
foreign key(SSN) references Person (SSN),
foreign key(PlateNr) references Car (PlateNr)
);
Insert into Person(SSN, Name) VALUES ('123456789','Max');
Insert into Person(SSN, Name) VALUES ('123456787','John');
Insert into Person(SSN, Name) VALUES ('123456788','Tom');
Insert into Car(PlateNr, Model) VALUES ('123ABC','Volvo');
Insert into Car(PlateNr, Model) VALUES ('321CBA','Toyota');
Insert into Car(PlateNr, Model) VALUES ('333AAA','Honda');
Insert into CarOwner(SSN, PlateNr) VALUES ('123456789','123ABC');
Insert into CarOwner(SSN, PlateNr) VALUES ('123456787','333AAA');
The problem I'm having is the SELECTE query I wanna make. 我遇到的问题是我想进行的SELECTE查询。 I wan't to be able to SELECT everything from the Person and wan't the include the PlateNr of the car he's the owner of, an example:
我将无法从“人”中选择所有内容,并且不包括他所拥有的汽车的PlateNr,例如:
PERSON 人
---------------------------------
SSN NAME Car
123456789 Max 123ABC
123456787 John 3338AAA
123456788 Tom
----------------------------------
So, I want to be able to show everything from the Person table and display the content of CarOwner aswell if the person is in fact a CarOwner. 因此,如果该人实际上是CarOwner,那么我希望能够显示Person表中的所有内容并显示CarOwner的内容。 What I have so far is: "SELECT * from Person, CarOwner WHERE Person.SSN = CarOwner.SSN;".
到目前为止,我所拥有的是:“ SELECT * from Person,CarOwner WHERE Person.SSN = CarOwner.SSN;”。 But this obviously results in only showing the person(s) that are CarOwners.
但这显然导致仅显示那些是车主的人。
Hope I explained me well enough, Thanks. 希望我对我的解释足够好,谢谢。
select ssn, name, car 选择ssn,名称,汽车
from Person p 从人p
LEFT OUTER JOIN CarOwner co 左外联接CarOwner co
ON p.SSN = co.SSN 开启p.SSN = co.SSN
LEFT OUTER JOIN Car c 左外联接车c
ON co.PlateNr = c.PlateNr ON co.PlateNr = c.PlateNr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.