首页 > 解决方案 > 在 INSERT INTO 问题中使用两个 SELECT FROM 语法

问题描述

我有一个名为ps_manufacturer_lang4 列的表,名为id_manufacturerandid_langdescriptionand short_description

  1. id_manufacturer应该是来自 的值SELECT id_manufacturer FROM ps_manufacturer

  2. id_lang应该是1

  3. description应该是来自的值SELECT description FROM prestashop_old.ps_category_lang WHERE id_lang='1' AND id_category IN ( SELECT id_category FROM prestashop_old.ps_category WHERE id_parent='241')

  4. short_description应该是NULL

这是我的代码:

INSERT INTO ps_manufacturer_lang (id_manufacturer, id_lang, description, short_description)
SELECT
    id_manufacturer
FROM ps_manufacturer
    1,
SELECT
    description
FROM prestashop_old.ps_category_lang 
WHERE 
    id_lang='1' 
    AND id_category IN (
        SELECT id_category FROM prestashop_old.ps_category WHERE id_parent='241'
    )
    NULL

错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1,
SELECT
    description
FROM prestashop_old.ps_category_lang
WHERE
    ' at line 5

似乎不可能SELECT FROM在一个中使用两种语法INSERT INTO

有什么帮助吗?

谢谢,

标签: mysqlsqldatabase

解决方案


嗯。. . 我怀疑你想要这样的逻辑。

INSERT INTO ps_manufacturer_lang (id_manufacturer, id_lang, description, short_description)
    SELECT m.id_manufacturer, 1, cl.description, NULL
    FROM ps_manufacturer m join
         prestashop_old.ps_category_lang  cl
         ON cl.id_lang = 1 and
            cl.id_category IN (
                SELECT id_category
                FROM prestashop_old.ps_category
                WHERE id_parent = 241
               );

测试第select一个,看看它是否返回你想要的。


推荐阅读