我有这个问题:
SELECT * FROM (
(SELECT ACCOUNTS.INSTALLTIME, ACCOUNTS.HONAME, ACCOUNTS.ADDRESS, ACCOUNTS.CITY, ACCOUNTS.STATE, ACCOUNTS.ZIP, ACCOUNTS.JOBNUMBER, ACCOUNTS.INSTALLDATE, ACCOUNTS.RESULT, ACCOUNTS.NOTES, ACCOUNTS.SMNOTES, technicians.technumber, technicians.boardplacing
FROM ACCOUNTS
INNER JOIN technicians ON ACCOUNTS.INSTALLER = technicians.technumber)
UNION
(SELECT service.servicetime, service.Customername, service.address, service.city, service.state, service.zip, service.ID, service.serviceday, service.result, service.servicenotes, service.board, technicians.technumber, technicians.boardplacing
FROM service
INNER JOIN technicians ON service.technician= technicians.technumber)
) as t WHERE t.INSTALLDATE = '$date' ORDER BY t.boardplacing
有什么方法可以进行类似的查询:
SELECT * FROM (
(SELECT ACCOUNTS.INSTALLTIME, ACCOUNTS.HONAME, ACCOUNTS.ADDRESS, ACCOUNTS.CITY, ACCOUNTS.STATE, ACCOUNTS.ZIP, ACCOUNTS.JOBNUMBER, ACCOUNTS.INSTALLDATE, ACCOUNTS.RESULT, ACCOUNTS.NOTES, ACCOUNTS.SMNOTES, '' as priority, ACCOUNTS.PAFS, ACCOUNTS.upsell, ACCOUNTS.TERM, ACCOUNTS.MMRUPGRADE, ACCOUNTS.WARRANTY, ACCOUNTS.EFT, technicians.technumber, technicians.boardplacing
FROM ACCOUNTS
INNER JOIN technicians ON ACCOUNTS.INSTALLER = technicians.technumber)
UNION
(SELECT service.servicetime, service.Customername, service.address, service.city, service.state, service.zip, service.ID, service.serviceday, service.result, service.servicenotes, service.board, '', '', '', '', '', '', technicians.technumber, technicians.boardplacing
FROM service
INNER JOIN technicians ON service.technician= technicians.technumber)
) as t WHERE t.INSTALLDATE = '$date' ORDER BY t.boardplacing
基本上我需要在我的工会中使用假列。有什么方法可以用连接来解决这个问题吗?有没有其他更好的方法来做到这一点?
答案 0 :(得分:4)
当我编写联合查询并需要“假”或“虚拟”列时,我只使用:
NULL AS Fake
所以整个查询看起来像这样;
SELECT A.CHEESE, A.BREAD, A.GARLIC, A.COST
FROM ACHEESETABLE A
WHERE A.BREAD = WHEAT
UNION ALL
SELECT NULL AS CHEESE, B.BREAD, NULL AS GARLIC, B.COST
FROM BCHEESYTABLE B
WHERE B.COST > 15
这样,两个查询都有CHEESE
和GARLIC
列,但BCHEESYTABLE
不包含CHEESE
或GARLIC
列。这样做也允许两个查询有不同的WHERE
条件,因此实质上可以在同一个查询中有两个不同的记录填充。