2013-05-14 69 views
1

我的表結構:MySQL的未知列(在表)

inventory_items:

CREATE TABLE `inventory_items` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
`name` varchar(50) DEFAULT NULL COMMENT 'Usually used as invoice number', 
`vendor_id` int(10) NOT NULL DEFAULT '1', 
`item_type_id` int(10) unsigned DEFAULT NULL, 
`item_model_id` int(10) unsigned DEFAULT NULL, 
`condition_id` int(10) unsigned DEFAULT NULL, 
`item_functionality_id` int(10) unsigned DEFAULT NULL, 
`color_id` int(10) unsigned DEFAULT NULL, 
`quantity` int(10) unsigned DEFAULT NULL, 
`original_qty` int(10) DEFAULT NULL, 
`note` text, 
`zone_id` int(10) unsigned DEFAULT NULL, 
`rack_id` int(10) unsigned DEFAULT NULL, 
`shelf_id` int(10) unsigned DEFAULT NULL, 
`bin_id` int(10) unsigned DEFAULT NULL, 
`status` char(1) DEFAULT NULL COMMENT '1:CheckedIn;2:Transferred', 
`reserve` tinyint(1) NOT NULL DEFAULT '0', 
`log` text, 
`user_id` int(10) unsigned DEFAULT NULL, 
`created` datetime DEFAULT NULL, 
`modified` datetime DEFAULT NULL, 
`item_manu_model_id` int(11) NOT NULL DEFAULT '0', 
`cdma_carrier_id` int(11) DEFAULT '0', 
PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=45334 DEFAULT CHARSET=latin1 

我的SQL查詢:

SELECT inventory_items.id, 
     conditions.`name`, 
     item_models.`name` 
FROM inventory_items, 
     sorting_reports 
     INNER JOIN item_models 
       ON inventory_items.item_model_id = item_models.id 
     INNER JOIN conditions 
       ON inventory_items.condition_id = conditions.id 
WHERE inventory_items.item_model_id = 1111 
     AND inventory_items.condition_id = 10 
     AND inventory_items.id = 20 
     AND ((inventory_items.zone_id = 21) 
       OR (inventory_items.rack_id = 175)) 
     AND sorting_reports.id != 0 

,我得到了以下錯誤:

[Err] 1054 - Unknown column 'inventory_items.item_model_id' in 'on clause' 

此列已存在... :(

+0

你不加入inventory_items的桌子上休息。您在FROM子句中的inventory_items之後需要加入語句。不知道它會解決問題,但它是一個開始。 – Robbert

回答

3

簡答:你的混合連接類型。使用Inner join,表示法,而不是兩者。 原因:編譯器嘗試在生成,類型聯接中可能較大的笛卡爾之前先完成內部聯接語句。因此,在嘗試加入時,它不知道有關的價值。如果你確實是指交叉加入狀態,那麼問題就應該消失。

更多內容,可以發現:What is the difference between using a cross join and putting a comma between the two tables?

SELECT inventory_items.id, 
     conditions.`name`, 
     item_models.`name` 
FROM inventory_items 
     CROSS JOIN sorting_reports 
     INNER JOIN item_models 
       ON inventory_items.item_model_id = item_models.id 
     INNER JOIN conditions 
       ON inventory_items.condition_id = conditions.id 
WHERE inventory_items.item_model_id = 1111 
     AND inventory_items.condition_id = 10 
     AND inventory_items.id = 20 
     AND ((inventory_items.zone_id = 21) 
       OR (inventory_items.rack_id = 175)) 
     AND sorting_reports.id != 0 
+0

(headbang)謝謝。現在工作.. –