简体   繁体   English

使用来自第三张表的共享ID更新具有空列的表和来自另一张表的数据

[英]Update Table With An Empty Column With Data From Another Table using a shared id from a third table

Hi Guys new to this and need pointed in the right direction. 嗨,新手,需要指出正确的方向。

DB1 product_description [product_id, name, description]

* DB1.product_description.product_id is linked to category_id in a separate product_to_category_id table. category_id in table1 == cat_id * 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 in DB2 .Inventory 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 Inventory [productId, name, description, cat_id] 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           |
---------------------------------

I am looking to copy the "description" data from DB2 and place it into the "description" in DB1 preferably using WHERE cat_id >=237 <=456 我希望从DB2复制“描述”数据并将其放入DB1中的“描述”,最好使用WHERE cat_id >=237 <=456

I was hoping to use the category id because I can move products over and insert meta dat at the same time. 我希望使用类别ID,因为我可以移动产品并同时插入meta dat。 cat_id is a collection of products around 200 or using the productId but I would need to update the other fields separately cat_id是大约200种产品或使用productId的产品的集合,但我需要分别更新其他字段

UPDATE DB1.product_description
SET description = (SELECT description
FROM DB2.Inventory
WHERE `cat_id` =2616);

it gives the error #1064 - You have an error in your SQL syntax; 它给出错误#1064-您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ' SET description = (SELECT description FROM DB2.Inventory WHERE cat_id =2616)' at line 2 在第2行上,检查与您的MySQL服务器版本相对应的手册,以在' SET description = (SELECT description FROM DB2.Inventory WHERE cat_id =2616)' at line 2 SET description = (SELECT description FROM DB2.Inventory WHERE附近)使用正确的语法

removed the comma thanks strawberry ;) now get the error; 删除逗号谢谢草莓;)现在得到的错误;

#1242 - Subquery returns more than 1 row

I have 30,000 products listed with descriptions, but need to integrate another 2000 products into the database, without disturbing products that have data already in the description field. 我列出了30,000种带有描述的产品,但需要将另外2000种产品集成到数据库中,而又不干扰描述字段中已有数据的产品。

I have tried various post on this site before posting but can't figure out how to use the cat_id form the separate table. 在发布之前,我已经尝试过此站点上的各种发布,但是无法弄清楚如何使用单独表中的cat_id。 Or even if its possible. 甚至可能。 If someone would be kind enough to point out where I'm going wrong i would be grateful. 如果有人愿意指出我要去哪里,我将不胜感激。 I have spent the last three days trying to get the description field filled but with no joy after viewing tons of posts on the forum 最近三天,我一直在尝试填写说明字段,但在查看论坛上的大量帖子后却不高兴

Thanks again 再次感谢

HTT HTT

Please try the following on your test environment: 请在您的测试环境中尝试以下操作:

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)
*/

I think Mr @Jay Blanchard pointed to the right direction. 我认为@Jay Blanchard先生指出了正确的方向。

Hope it may help at least a little. 希望它至少可以有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM