MySql 插入查询具有 select 语句的值无效

MySql Insert query having a select statement for a value not working

我有一个插入查询,其中一个值需要是从 select 语句返回的管道分隔字符串。 Select statement 和 insert 都独立工作,但是 mySql 在合并两个查询时给出语法错误。

这是我的插入查询

INSERT INTO sales (site_id, name, start_date, end_date, per_item_discount, enabled,entry_ids) 
VALUES (1, 'auto', 1448662620, 1448749020, 2.0000, 1,
        (SELECT GROUP_CONCAT(DISTINCT entry_id SEPARATOR '|') 
         FROM data
         JOIN relationships ON data.entry_id = relationships.parent_id
         WHERE (relationships.child_id = 18 AND data.id_5 = 'VHK-SG') ))

现在这两个查询单独使用,select 语句和使用硬编码 entry_ids 插入,如 '1|2',工作完美但组合不是。

我不确定我在这里遗漏了什么。

感谢任何帮助。

您需要更改语法。检查 this

你应该这样做:

INSERT INTO sales 
(site_id,name,start_date,end_date,per_item_discount,enabled,entry_ids) 
SELECT 1,'auto',1448662620,1448749020,2.0000,1,GROUP_CONCAT(DISTINCT entry_id SEPARATOR '|')
FROM data JOIN relationships ON data.entry_id = relationships.parent_id 
WHERE (relationships.child_id = 18 AND data.id_5 = 'VHK-SG')