简体   繁体   English

MySQL INSERT INTO查询列值等于另一个SQL SELECT查询

[英]MySQL INSERT INTO query column value equals another SQL SELECT query

Could anybody kindly guide me on correct direction for below query? 有人可以指导我进行以下查询的正确方向吗? It's not working under phpMyAdmin. 在phpMyAdmin下不起作用。

INSERT INTO `Setting` 
(`id`, `type`, `name`, `value`, `parentId`, `createdAt`, `updatedAt`, `createdById`, `updatedById`) 
VALUES 
(NULL, 0, 'howItWorks', 'Some URL', NULL, NULL, NULL, -1, NULL), 
(NULL, 0, 'howItWorksThumb', 'Some URL', (SELECT id FROM Setting WHERE name = 'howItWorks'), NULL, NULL, -1, NULL);

Same kind of query works under PostgreSQL. PostgreSQL可以使用相同的查询。

Error I am getting: #1093 - You can't specify target table 'Setting' for update in FROM clause 我得到的错误是: #1093-您无法在FROM子句中指定目标表“设置”以进行更新

Question is update to explained issues related to LAST_INSERT_ID() solutions: 问题是对与LAST_INSERT_ID()解决方案有关的解释问题的更新:

To use LAST_INSERT_ID() solutions; 使用LAST_INSERT_ID()解决方案; child row should be inserting immediately after parent row. 子行应紧接在父行之后。

I want to get the parentId for child row not immediately after I insert parent row. 我想在插入父行后不立即获取子行的parentId。 What will be the solution? 解决办法是什么?

Also what if I want to add two children for same parent row? 如果我想为同一父行添加两个孩子怎么办?

MySQL doesn't allow you to SELECT in a subquery from the same table that you're inserting into in the main query. MySQL不允许您从要插入主查询的同一表中的子查询中进行SELECT So you'll need to split this into two INSERT queries: 因此,您需要将其分为两个INSERT查询:

You can use LAST_INSERT_ID() to get the auto-increment ID that was assigned in the last INSERT , rather than using a subquery. 您可以使用LAST_INSERT_ID()来获取在上一次INSERT分配的自动增量ID,而不是使用子查询。

INSERT INTO `Setting` (`id`, `type`, `name`, `value`, `parentId`, `createdAt`, `updatedAt`, `createdById`, `updatedById`) 
    VALUES (NULL, 0, 'howItWorks', 'Some URL', NULL, NULL, NULL, -1, NULL);
INSERT INTO `Setting` (`id`, `type`, `name`, `value`, `parentId`, `createdAt`, `updatedAt`, `createdById`, `updatedById`) 
    VALUES (NULL, 0, 'howItWorksThumb', 'Some URL', LAST_INSERT_ID(), NULL, NULL, -1, NULL);

Unfortunately, using LAST_INSERT_ID() still doesn't allow you to combine them into a single query, because it calls the function before doing any inserts. 不幸的是,使用LAST_INSERT_ID()仍然不允许您将它们组合到单个查询中,因为它会在执行任何插入操作之前调用该函数。

If you're doing the second insert later, you can do it with a normal INSERT ... SELECT ... : 如果稍后要进行第二次插入,则可以使用普通的INSERT ... SELECT ...

INSERT INTO `Setting` (`id`, `type`, `name`, `value`, `parentId`, `createdAt`, `updatedAt`, `createdById`, `updatedById`) 
SELECT NULL, 0, 'howItWorksThumb', 'Some URL', id, NULL, NULL, -1, NULL
FROM Setting
WHERE name = 'howItWorks'
DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table 
( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
, type TINYINT NOT NULL
, name VARCHAR(100) NOT NULL
, value VARCHAR(100) NOT NULL
, parentId INT NULL
, createdById INT NOT NULL
);

It would be sensible to bind the following into a transaction. 将以下内容绑定到事务中将是明智的。

INSERT INTO my_table 
( type
, name
, value
, createdById
) 
VALUES 
( 0
, 'howItWorks'
, 'Some URL'
, -1
);

INSERT INTO my_table 
( type
, name
, value
, parentId
, createdById
)
SELECT 0
     , 'howItWorksThumb'
     , 'Some URL'
     , LAST_INSERT_ID()
     , -1
  FROM my_table;

End of transaction 交易结束

  SELECT * FROM my_table;
  +----+------+-----------------+----------+----------+-------------+
  | id | type | name            | value    | parentId | createdById |
  +----+------+-----------------+----------+----------+-------------+
  |  1 |    0 | howItWorks      | Some URL |     NULL |          -1 |
  |  2 |    0 | howItWorksThumb | Some URL |        1 |          -1 |
  +----+------+-----------------+----------+----------+-------------+

see the data type of the table that you created. 查看您创建的表的数据类型。 and see also DEFAULT on the field that you create. 并在您创建的字段上查看默认值。 If ID is NULL should DEFAULT = auto_increment. 如果ID为NULL,则DEFAULT = auto_increment。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM