Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

sql - 这是具有联接的有效相关子查询吗?(Is this a valid correlated sub query with joins?)

So I have a table Car(Car_id, Value, Make) , another table Person(Person_id, Name) and a third table linking the two together PerCar(Car_id, Person_id) .

(所以我有一个表Car(Car_id, Value, Make) ,另一个表Person(Person_id, Name)和第三个表,将两者链接在一起PerCar(Car_id, Person_id) 。)

So I have this correlated subquery:

(所以我有这个相关的子查询:)

SELECT MAKE, VALUE 
FROM CAR WHERE VALUE > (SELECT AVG(VALUE) FROM CAR C WHERE C.CAR_ID = CAR_ID);

So if I wanted to display the person's name also, is this also valid correlated sub query:

(因此,如果我也想显示此人的姓名,这是否也是有效的相关子查询:)

SELECT Car.Make, Car.Value, Person.Name 
FROM PerCar NATURAL JOIN Car 
NATURAL JOIN Person where Car.Value > (SELECT AVG(VALUE) FROM Car);
  ask by Drakus translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your query should be as below-

(您的查询应如下-)

You can check DEMO HERE

(您可以在此处检查演示)

SELECT Car.Make, 
Car.Value, 
Person.Name 
from Car
INNER JOIN CarPer 
    ON Car.Car_id = CarPer.Car_id 
INNER JOIN Person
    ON Person.Person_id = CarPer.Person_ID        
WHERE Car.Value > (SELECT AVG(VALUE) FROM Car);

Remember, here joining can be INNER, OUTER, LEFT or other joins based on your requirement and data structure.

(请记住,根据您的要求和数据结构,这里的连接可以是INNER,OUTER,LEFT或其他连接。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...