MySQL/INSERT INTO:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
无编辑摘要
Kangkk​(留言 | 贡献)
第21行: 第21行:
=== 边查边插 ===
=== 边查边插 ===
{{Shc_out|<syntaxhighlight lang="mysql">
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT INTO table_1(field_1, field_2)
INSERT INTO table_1 (
SELECT field_3 as field_1, field_4 as field_2 FROM table_2  
    field_1,
WHERE 1 = 1;</syntaxhighlight>|<syntaxhighlight lang="bash">
    field_2
  )
SELECT
    field_3 AS field_1,
    field_4 as field_2
FROM
    table_2  
WHERE
    1 = 1;</syntaxhighlight>|<syntaxhighlight lang="bash">
</syntaxhighlight>|mysql>}}
</syntaxhighlight>|mysql>}}


=== 多表关联边查边插 ===
=== 多表关联边查边插 ===
{{Shc_out|<syntaxhighlight lang="mysql">
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT INTO table_1(field_1, field_2)
INSERT INTO table_1 (
SELECT a.field_3 as field_1, b.field_4 as field_2 FROM table_2 AS a
    field_1,
LEFT JOIN table_3 as b ON a.filed = b.filed
    field_2
  )
SELECT
    a.field_3 AS field_1,
    b.field_4 AS field_2
FROM
    table_2 AS a
LEFT JOIN
    table_3 as b
      ON a.filed = b.filed
WHERE 1 = 1;</syntaxhighlight>|<syntaxhighlight lang="bash">
WHERE 1 = 1;</syntaxhighlight>|<syntaxhighlight lang="bash">
</syntaxhighlight>|mysql>}}
</syntaxhighlight>|mysql>}}
第52行: 第69行:


{{Shc_out|<syntaxhighlight lang="mysql">
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT ignore INTO user(id, name, email) values(3, 'kk', 'kk@kangkk.com');</syntaxhighlight>|<syntaxhighlight lang="bash">
INSERT ignore INTO user (
    id,
    name,
    email
  )
VALUES (
    3,
    'kk',
    'kk@kangkk.com'
  );</syntaxhighlight>|<syntaxhighlight lang="bash">
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
</syntaxhighlight>|mysql>}}
</syntaxhighlight>|mysql>}}
第74行: 第100行:
如果数据(主键或唯一索引)存在,则执行更新操作。
如果数据(主键或唯一索引)存在,则执行更新操作。
{{Shc_out|<syntaxhighlight lang="mysql">
{{Shc_out|<syntaxhighlight lang="mysql">
INSERT INTO user(id, name, email) values(3, 'kk', 'kk@kangkk.com') on duplicate key update name='kk', email='kk@kangkk.com';</syntaxhighlight>|<syntaxhighlight lang="bash">
INSERT INTO user (
    id,
    name,
    email
  )
VALUES (
    3,
    'kk',
    'kk@kangkk.com'
  )
ON DUPLICATE KEY UPDATE
    name = 'kk',
    email = 'kk@kangkk.com';</syntaxhighlight>|<syntaxhighlight lang="bash">
Query OK, 2 rows affected (0.01 sec)
Query OK, 2 rows affected (0.01 sec)
</syntaxhighlight>|mysql>}}
</syntaxhighlight>|mysql>}}
第97行: 第135行:


{{Shc_out|<syntaxhighlight lang="mysql">
{{Shc_out|<syntaxhighlight lang="mysql">
REPLACE INTO user(id, name, email) values(3, 'lan', 'lan@kangkk.com');</syntaxhighlight>|<syntaxhighlight lang="bash">
REPLACE INTO user (
    id,
    name,
    email
  )
VALUES (
    3,
    'lan',
    'lan@kangkk.com'
  );</syntaxhighlight>|<syntaxhighlight lang="bash">
Query OK, 2 rows affected (0.00 sec)
Query OK, 2 rows affected (0.00 sec)
</syntaxhighlight>|mysql>}}
</syntaxhighlight>|mysql>}}
第137行: 第184行:


{{Shc_out|<syntaxhighlight lang="bash">
{{Shc_out|<syntaxhighlight lang="bash">
INSERT INTO user(name, email)
INSERT INTO user (
    name,
    email
  )
SELECT
SELECT
     'kk',
     'kk',
     'kk@kangkk.com'
     'kk@kangkk.com'
FROM user
FROM
    user
WHERE not exists (
WHERE not exists (
     SELECT name
     SELECT name