【发布时间】:2011-11-21 15:29:21
【问题描述】:
我有以下查询:
SELECT * FROM
(
SELECT
catalog.*,
images.image
FROM `catalog` as catalog
INNER JOIN `type5` as images ON catalog.id = images.id
WHERE catalog.left_key > (SELECT `left_key` FROM `catalog` WHERE `id`=235) AND catalog.right_key < (SELECT `right_key` FROM `catalog` WHERE `id`=235)
ORDER BY catalog.left_key ASC
) ilv
此查询运行良好,但仅当表 type5 中的项目 ID 相同时,它才会从目录中选择项目。
例如在目录中我有:
id name
5 Hello
7 World
8 Foo
9 Bar
在类型 5 中,我有: 身份证图片
5 hello.png
8 foo.png
9 bar.png
它只会选择
5 Hello
8 Foo
9 Bar
即使 type5 中没有相同的 id,如何编辑我的查询以从目录中选择项目?
【问题讨论】:
-
您需要的是外连接。请参阅
join (sql)上的维基百科页面。
标签: mysql sql join inner-join