我刚刚开始学习数据库和SQL,并且我一直在研究一个我无法完全确定的问题。给定两个名为CHARTER和CUSTOMER的表,这就是我要尝试的操作:

Give the relational algebra statement (or SQL) as well as the table that would
result from applying SELECT & JOIN relational operators to the CHARTER and
CUSTOMER tables to return only the CHAR_TRIP, CUS_LNAME and CHAR_DESTINATION
attributes for charters flown by pilot 109.

Note that 109 is sometimes the pilot and other times the co-pilot.
Display all these flights.


这是我尝试过的SQL:

select CHAR_TRIP, CUS_LNAME, CHAR_DESTINATION from CHARTER natural join CUSTOMER where CHARTER.CHAR_PILOT and CHARTER.CHAR_COPILOT="109";


但这似乎并没有给我我想要的东西。我应该得到6条记录,但是我只有3条记录。我认为这可能是由于SQL中的某些原因。我是否忽略了代码中的某些内容?

最佳答案

不知道Mysql是否具有某些特殊语法,但是请尝试分离WHERE条件:

select CHAR_TRIP, CUS_LNAME, CHAR_DESTINATION
from CHARTER natural join CUSTOMER
where CHARTER.CHAR_PILOT ="109"
or CHARTER.CHAR_COPILOT="109";


我将第二个更改为or,因为109既可以是主驾驶员也可以是副驾驶员。

关于mysql - SQL选择和联接表以显示结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48728392/

10-12 13:05