我越来越沮丧,因为我不明白为什么在我的 SELECT with UNION 中我缺少一些字段。我阅读了有关此的良好文档,并遵循了最常见的示例,以避免在第一个和第二个 SELECT 之间获得不同数量的列。
它包括在两个语句之间添加达到偶数列的 NULL 列。
这是我的情况:
$query = "(SELECT GC.GroupNO AS GroupNO,
GC.Name AS Name,
GC.Pax AS Pax,
GC.Start AS Start,
NULL AS custid,
NULL AS firstname,
NULL AS lastname,
NULL AS arrivaldate,
NULL AS hour,
NULL AS pax,
NULL AS cctype,
NULL AS created,
NULL AS sid,
NULL AS ref_source,
NULL AS numnights
FROM LargeGroupHostels, GroupContacts AS GC
WHERE LargeGroupHostels.HostelNumber = %d
AND LargeGroupHostels.Status = %s
AND LargeGroupHostels.GroupNO = GC.GroupNO";
AND GC.Start < %s
ORDER BY LastUpdated DESC LIMIT %d, %d
)UNION(
SELECT
NULL AS GroupNO,
NULL AS Name,
NULL AS Pax,
NULL AS Start,
`custid`,
`firstname`,
`lastname`,
`arrivaldate`,
`hour`,
`pax`,
`cctype`,
`created`,
`sid`,
`ref_source`,
datediff(departdate, arrivaldate) AS numnights
FROM customer_details
WHERE hostelnumber = %d
AND lastupdated = "" AND status != "CANX"
AND booking_type IN ("HOSTEL", "HOSTELPACK")
ORDER BY customer_details.arrivaldate, customer_details.created
LIMIT %d, %d )
';
除了我知道要做的一些优化和一些更改之外,我的问题是: - 为什么我没有得到这些列GC.Name,GC.Pax,GC.Start
?如果我反转这两个 SELECT,它会发生完全相反的情况,只考虑GC.Name,GC.Pax,GC.Start
并避免其他列。
我对这种查询不太熟悉,这是我第一次处理这个问题。我确定我错过了一些东西,但我不明白是什么