I’m trying to insert records from other tables into store_orders_items.
For example:
INSERT INTO store_orders_items( order_id, sel_item_id, sel_item_price)
SELECT order_id, sel_item_id, sel_item_price
FROM orders_ids
INNER JOIN store_orders ON store_orders.id = orders_ids.order_id
INNER JOIN store_shoppertrack ON store_items.id=store_shoppertrack.sel_item_id
Where session_id = '".$_COOKIE["PHPSESSID"]."';
Then I received the following message: Unknown column ‘sel_item_price’ in ‘field list’. I tried to set sel_item_price As item_price to no avail.
Here are my tables:
store_orders_items:
id| order_id | sel_item_id |sel_item_price|
-------------------------------------------
| | | |
store_shoppertrack:
id| session_id | sel_item_id |date_added|
-------------------------------------------
| | | |
store_orders:
id| item_total| order_date|
---------------------------
| | |
store_items:
id| item_price| item_color|
---------------------------
| | |
orders_ids:
id| order_id | status|
----------------------
| | |
There is no table in jour join which has either a column named
sel_item_price, nor a columnitem_price. TheINSERT INTOhas nothing to do with this, theSELECTon its own will fail just as well. I assume that you wanted to join with thestore_itemstable as well, and select theitem_pricecolumn from that table for insertion into thesel_item_pricetable ofstore_order_items. You could rewrite yourSELECTlike this:The
AS sel_item_priceis optional, since you already specified the destination column in theINSERT INTOpart. But it might help consistency, as the output of theSELECTwill now hace volumns labeled the same as those of the target table. And it exhibits that you got the order of theASwrong in your question, so you might learn something here.I’ve created a SQL Fiddle for your schema and query.