且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用子查询 MySQL 插入 INTO

更新时间:2023-02-26 09:41:58

SELECT 语句中使用带有别名的数字文字.SELECT 组件周围不需要 ().

Use numeric literals with aliases inside a SELECT statement. No () are necessary around the SELECT component.

INSERT INTO qa_costpriceslog (item_code, invoice_code, item_costprice)
  SELECT
    /* Literal number values with column aliases */
    1 AS item_code,
    2 AS invoice_code,
    item_costprice
  FROM qa_items 
  WHERE item_code = 1;

请注意,在 INSERT INTO...SELECT 的上下文中,别名实际上不是必需的,您只需 SELECT 1, 2, item_costprice,但在正常的 SELECT 你需要别名来访问返回的列.

Note that in context of an INSERT INTO...SELECT, the aliases are not actually necessary and you can just SELECT 1, 2, item_costprice, but in a normal SELECT you'll need the aliases to access the columns returned.