我有一个有趣的查询,我似乎遇到了麻烦。
我需要从几个表中选择数据并加入另一个表,该表的列可能不存在于其他表中。
我已经尝试了几个角度。这是第一个:
SELECT SiteTree.*, Page.*, AlbumItem.*, Album.*
FROM SiteTree, AlbumItem, Album
LEFT JOIN Page ON SiteTree.ID = Page.ID
抛出这个:#1054 - 'on 子句'中的未知列 'SiteTree.ID',大概是因为当它从相册表中选择时尝试加入时,没有 SiteTree.ID 列。
我也试过这个:
SELECT SiteTree.*, Page.*, AlbumItem.*, Album.*
FROM SiteTree LEFT JOIN Page ON SiteTree.ID = Page.ID, AlbumItem, Album
它返回大约 20 行,我知道根据 where 子句应该只有 2 行。事实上,每个结果行大约有 10 个副本
where子句如下:
WHERE (
(
AlbumItem.ClassName = 'AlbumItem' OR
Album.ClassName = 'Album' OR
SiteTree.ClassName = 'Page' OR
SiteTree.ClassName = 'HomePage' OR
SiteTree.ClassName = 'NewsCategory' OR
SiteTree.ClassName = 'NewsArticle' OR
SiteTree.ClassName = 'CartPage' OR
SiteTree.ClassName = 'Product' OR
SiteTree.ClassName = 'ProductGroup' OR
SiteTree.ClassName = 'MemberProfilePage' OR
SiteTree.ClassName = 'SubscriptionPage' OR
SiteTree.ClassName = 'ErrorPage' OR
SiteTree.ClassName = 'RedirectorPage' OR
SiteTree.ClassName = 'VirtualPage' OR
SiteTree.ClassName = 'UserDefinedForm' OR
SiteTree.ClassName = 'CheckoutPage' OR
SiteTree.ClassName = 'OrderConfirmationPage' OR
SiteTree.ClassName = 'TagPage'
) AND (
INSTR(Page.Tags,'x') OR
INSTR(AlbumItem.Tags,'x') OR
INSTR(Album.Tags,'x')
)
)
将查询减少到这个会产生正确的结果,但它也不适用于其他表:
SELECT SiteTree.*, Page.*
FROM SiteTree
LEFT JOIN Page ON SiteTree.ID = Page.ID
我需要知道的是,如果不诉诸工会,我正在尝试做的事情是否可行?