當'產品'表(master_categories_id = 500)滿足條件時,我試圖在表'products_to_categories'中插入一個關係我的猜測是因爲我的SELECT語句應該是一個JOIN?mySQL'無效列'錯誤
table: products_to_categories
columns: products_id, categories_id
table: products
columns: products_id, master_categories_id
INSERT INTO products_to_categories (products_id, categories_id)
SELECT products.products_id, 500 FROM products
WHERE products.master_categories_id=500 HAVING count(categories_id) < 1;
錯誤返回:categories_id is an invalid column
是的我試過了,categories_id不在'產品'表中。這就是爲什麼我猜測我需要用JOIN重寫。或者你可以簡單地在SELECT中說products_to_categories.categories_id? – 2012-04-13 23:54:29
我只想用一個連接 – 2012-04-13 23:57:29
這是我最好的猜測,原諒我,通常我只是用PHP來處理這種事情,試圖用更高級的查詢來獲得更多的舒適,以避免腳本編寫。 INSERT INTO products_to_categories(products_id,categories_id) SELECT products.products_id,products.master_categories_id,products_to_categories.categories_id INNER JOIN產品ON products.products_id = products_to_categories.products_id FROM產品WHERE products.master_categories_id = 500,其具有計數(categories_id)< 1; – 2012-04-14 00:10:54