使用来自第三个表的共享ID,使用来自另一个表的数据的空列更新表

时间:2015-06-05 12:34:30

标签: php mysql database

嗨,大家好新了,需要指出正确的方向。

DB1 product_description [product_id, name, description]

* DB1.product_description.product_idcategory_id product_to_category_id table. category_id in table1 == cat_id DB2中的 DB1 product_description ------------------------------------------------------ | product_id | name | description | ------------------------------------------------------- | 999 | product999 | description text | | 1000 | product1000 | | | 1001 | product1001 | | | 2000 | product2000 | | ------------------------------------------------------- 相关联。广告资源

[productId, name, description, cat_id]

DB2 Inventory 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 | ---------------------------------

WHERE cat_id >=237 <=456

我希望复制&#34;描述&#34;来自DB2的数据并将其放入&#34;描述&#34;在DB1中最好使用cat_id

我希望使用类别ID,因为我可以移动产品并同时插入元数据。 UPDATE DB1.product_description SET description = (SELECT description FROM DB2.Inventory WHERE `cat_id` =2616); 是大约200种产品的集合 或使用productId,但我需要单独更新其他字段

SET description = (SELECT description FROM DB2.Inventory WHERE

它给出了错误  #1064 - 您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在=2616)' at line 2 cat_id #1242 - Subquery returns more than 1 row

附近使用正确的语法

删除逗号感谢草莓;)现在得到错误;

prepareForSegue

我有30,000个产品列出了描述,但是需要将另外2000个产品集成到数据库中,而不会干扰描述字段中已有数据的产品。

我在发布之前尝试过本网站上的各种帖子,但无法弄清楚如何将cat_id用于单独的表格。或者即使它可能。如果有人能够指出我出错的地方,我将不胜感激。我花了最后三天试图填写描述字段,但在论坛上查看大量帖子后没有任何喜悦

再次感谢

HTT

1 个答案:

答案 0 :(得分:0)

请在您的测试环境中尝试以下操作:

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先生指出了正确的方向。

希望它至少可以帮助一点。