我当前的查询是:
select
item_description.fk_i_item_id, item_description.s_title, item_description.s_description,
category_description.s_slug,
item_resource.s_path, item_resource.s_extension, item_resource.pk_i_id
FROM
item, item_description, category_description, item_resource
WHERE
item.pk_i_id = item_description.fk_i_item_id
AND
category_description.fk_i_category_id = 96
AND
item.pk_i_id = item_resource.fk_i_item_id
GROUP BY item.pk_i_id
ORDER BY item.pk_i_id DESC
基本上这些表存储了一些项目的信息(可选信息存储在 item_resource 中),我正在尝试从所有这4个表中获取所有有用的信息,当项目有<时它很好用em> resource ( item_resource 中的可选信息),但如果该项目没有资源,则该项目不会显示在结果上。
所以我需要一个结果,无论是否在item_resource表上有信息,都会返回该项。
答案 0 :(得分:1)
所以我需要一个结果,无论项目是否有,都会返回该项目 有关item_resource表的信息。
然后考虑做一个OUTER JOIN
,最有可能是LEFT OUTER JOIN
,而不是做INNER JOIN
。当前正在执行的是内部联接,当您说FROM item, item_description, category_description, item_resource
时,它是旧式联接语法。您应该考虑使用ANSI样式JOIN
语法。
此外,由于您没有执行任何汇总,因此不需要GROUP BY item.pk_i_id
。
我的意思是下面的内容:
select
item_description.fk_i_item_id, item_description.s_title,
item_description.s_description,
category_description.s_slug,
item_resource.s_path, item_resource.s_extension, item_resource.pk_i_id
FROM
item
LEFT JOIN item_description ON item.pk_i_id = item_description.fk_i_item_id
LEFT JOIN category_description ON category_description.some_column = item.pk_i_id
AND category_description.fk_i_category_id = 96
LEFT JOIN item_resource ON item.pk_i_id = item_resource.fk_i_item_id
ORDER BY item.pk_i_id DESC;