SQL:未知列'items.id'关系查询

时间:2015-08-24 21:27:00

标签: mysql sql

我对此查询有疑问。我认为问题在于找不到 items 。之后我正在做LEFT OUTER JOIN,但之前没什么。在项目开头包含项目表的最佳解决方案是什么?

SELECT COUNT(*) AS `numrows` FROM (`categories_items`) 
LEFT OUTER JOIN `items_stones` `items_stones` ON `items`.`id` = `items_stones`.`item_model_id` 
LEFT OUTER JOIN `items` ON `items`.`id` = `categories_items`.`item_model_id` 
WHERE ( `items_stones`.`stone_model_id` = 1 ) AND `categories_items`.`category_model_id` = 1

1 个答案:

答案 0 :(得分:1)

将您的查询更改为如下所示,基本上交换LEFT JOINS

    SELECT COUNT(*) AS `numrows` 
    FROM `categories_items` 
    LEFT OUTER JOIN `items` ON `items`.`id` = `categories_items`.`item_model_id`
   AND `categories_items`.`category_model_id` = 1
    LEFT OUTER JOIN `items_stones` ON `items`.`id` = `items_stones`.`item_model_id` 
 AND `items_stones`.`stone_model_id` = 1;