我有以下表格
customer
custID | name | address
并且
magazinesubscription
customerID | magazinename | cost | startdate | enddate
newspapersubscription
customerID | newspapername | cost | startdate | enddate
newspaper
newspapername | cost | publishedby
magazine
magazinename | cost | publishedby
现在我想要为每位顾客打印,他们的姓名,地址,他们的杂志订阅,他们的报纸订阅,包括杂志和报纸的名称和出版商以及每个订阅的结束日期。
我尝试过这样做,但它不起作用。
select * from customer
JOIN
(select customerID, newspapername, enddate, n.publishedby
from newspapersubscription ns and newspaper n
where published in(select publishedby
from newspaper
where ns.newspapername = n.newspapername)
UNION
select customerID, magazinename, enddate, m.publishedby
from magazinesubscription ms and magazine m
where published in(select publishedby
from magazine
where ms.newspapername = m.newspapername));
答案 0 :(得分:0)
我不确定您要对上述查询做什么。但我可以在您编写的查询中指出错误。
and
不是从不同表中获取数据的有效sql关键字。
(select customerID, newspapername, enddate, n.publishedby
from newspapersubscription ns, newspaper n
where published in(select publishedby
from newspaper
where ns.newspapername = n.newspapername)
UNION
select customerID, magazinename, enddate, m.publishedby
from magazinesubscription ms, magazine m
where published in(select publishedby
from magazine
where ms.newspapername = m.newspapername));
获得上述查询的结果后。您无需任何条件即可加入客户表。这将表现为两个表的笛卡尔积,从而组合所有数据。