mysql 查询一个表部分字段,然后插入另一个表中

现有表jcs_food_distribution,jcs_food,从jcs_food中查询food_code,enterprise_code,将其插入到jcs_food_distribution中

INSERT INTO jcs_food_distribution (food_code, enterprise_code) SELECT
    food_code,
    enterprise_code
FROM
    jcs_food
如果只需要导入jcs_food_distribution中不存在的记录,可以用

INSERT INTO jcs_food (food_code, enterprise_code) SELECT
    food_code,
    enterprise_code
FROM
    jcs_food
WHERE
    NOT EXISTS (
        SELECT
           food_code
        FROM
            jcs_food_distribution
        WHERE
            jcs_food_distribution.food_code = jcs_food.food_code
    )


你可能感兴趣的:(mysql 查询一个表部分字段,然后插入另一个表中)