[英]Moving fields to another table and using that id to update rows in a third table
[英]Update Table With An Empty Column With Data From Another Table using a shared id from a third table
嗨,新手,需要指出正確的方向。
DB1 product_description [product_id, name, description]
* DB1.product_description.product_id
在單獨的product_to_category_id table. category_id in table1 == cat_id
鏈接到category_id
product_to_category_id table. category_id in table1 == cat_id
product_to_category_id table. category_id in table1 == cat_id
DB2
cat_id
DB1 product_description
------------------------------------------------------
| product_id | name | description |
-------------------------------------------------------
| 999 | product999 | description text |
| 1000 | product1000 | |
| 1001 | product1001 | |
| 2000 | product2000 | |
-------------------------------------------------------
DB2庫存[productId, name, description, cat_id]
DB2
Inventory
------------------------------------------------------------------
| productId | name | description | cat_id |
-----------------------------------------------------------------
| 999 | product999 | description text | 236 |
| 1000 | product1000 | description text2 | 237 |
| 1001 | product1001 | description text3 | 237 |
| 2000 | product2000 | description text4 | 456 |
-----------------------------------------------------------------
DB1
product_to_category
---------------------------------
| product_id | category_id |
---------------------------------
| 999 | 236 |
| 1000 | 237 |
| 1001 | 237 |
| 2000 | 456 |
---------------------------------
我希望從DB2復制“描述”數據並將其放入DB1中的“描述”,最好使用WHERE cat_id >=237 <=456
我希望使用類別ID,因為我可以移動產品並同時插入meta dat。 cat_id
是大約200種產品或使用productId的產品的集合,但我需要分別更新其他字段
UPDATE DB1.product_description
SET description = (SELECT description
FROM DB2.Inventory
WHERE `cat_id` =2616);
它給出錯誤#1064-您的SQL語法有錯誤; 在第2行上,檢查與您的MySQL服務器版本相對應的手冊,以在' SET description = (SELECT description FROM DB2.Inventory WHERE
cat_id =2616)' at line 2
SET description = (SELECT description FROM DB2.Inventory WHERE
附近)使用正確的語法
刪除逗號謝謝草莓;)現在得到的錯誤;
#1242 - Subquery returns more than 1 row
我列出了30,000種帶有描述的產品,但需要將另外2000種產品集成到數據庫中,而又不干擾描述字段中已有數據的產品。
在發布之前,我已經嘗試過此站點上的各種發布,但是無法弄清楚如何使用單獨表中的cat_id。 甚至可能。 如果有人願意指出我要去哪里,我將不勝感激。 最近三天,我一直在嘗試填寫說明字段,但在查看論壇上的大量帖子后卻不高興
再次感謝
HTT
請在您的測試環境中嘗試以下操作:
UPDATE
DB1.product_description
INNER JOIN DB2.Inventory
ON DB2.Inventory.productId = DB1.product_description.product_id
SET
DB1.product_description.description = DB2.Inventory.description
WHERE
DB2.Inventory.cat_id = 2616;
/*
or for the range of records:
WHERE (DB2.Inventory.cat_id >= 237 AND DB2.Inventory.cat_id <= 456)
*/
我認為@Jay Blanchard先生指出了正確的方向。
希望它至少可以有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.