我有以下代码
SELECT *
FROM
customer
INNER JOIN (
SELECT
customerid, newspapername, enddate, n.publishedby
FROM
newspapersubscription ns,
newspaper n
WHERE
publishedby IN (
SELECT publishedby
FROM newspaper
WHERE ns.newspapername = n.NewspaperName
)
UNION
SELECT
customerid, Magazinename, enddate, m.publishedby
FROM
magazinesubscription ms,
magazine m
WHERE
publishedby IN (
SELECT publishedby
FROM magazine
WHERE ms.Magazinename = m.MagazineName
)
) ON
customer.customerid = customerid
ORDER BY
customer.customerid;
客户表具有以下内容:
customerid | customername | customersaddress
此查询返回以下结果:
customerid | customername | customersaddress | customerid | newspapername | enddate| publishedby
我实际想要的是
customerid | customername | customersaddress | newspapername | magazinename | enddate| publishedby
这里,如果magazinename存在,则newspapername字段应为空,反之亦然。此外,在联合操作中的customerid重复字段不应存在,而在我的结果中,newspapername和magazinename的值都放在newspapername标题下。
我该怎么做呢?