数据不会使用insert into和right join插入表中

时间:2017-02-27 20:35:21

标签: mysql insert-update insert-into right-join

我试图将Right join的结果插入我创建的表deal_classification_DM的CATO_NAME列中。 select的结果是我期望的,程序运行正常,但数据没有更新到表中。 怎么了?

`INSERT INTO deal_classification_DM
(CATO_NAME)
SELECT
    taxonomies.NAME
FROM 
    taxonomies
RIGHT JOIN 
    deal_classification_DM ON taxonomies.ID = deal_classification_DM.CATO_ID;

插入是在我创建的表deal_classification_DM中完成的:

CREATE TABLE deal_classification_DM 
AS SELECT 
    deal_taxonomy.DEAL_ID
    ,deal_taxonomy.TAXONOMY_ID AS TAXO_ID
    ,taxonomies.NAME AS TAXO_NAME
    ,taxonomies.PARENT_ID AS CATO_ID 
FROM
    deal_taxonomy
LEFT JOIN
    taxonomies ON deal_taxonomy.TAXONOMY_ID = taxonomies.ID;

ALTER TABLE deal_classification_DM
ADD COLUMN 
    CATO_NAME TEXT;

1 个答案:

答案 0 :(得分:0)

您真正需要做的是insert而不是UPDATE deal_classification_DM d SET d.CATO_NAME = ( SELECT name FROM taxonomies WHERE ID = d.CATO_ID ); ,例如:

INSERT

null语句将在DEAL_IDTAXO_IDCATO_ID列中插入包含CATO_NAME值的新行,而您只需要更新{{{ 1}}列,因为它是一个新列。