<tfoot id='XSNLQ'></tfoot>
  1. <legend id='XSNLQ'><style id='XSNLQ'><dir id='XSNLQ'><q id='XSNLQ'></q></dir></style></legend>
    1. <small id='XSNLQ'></small><noframes id='XSNLQ'>

      <i id='XSNLQ'><tr id='XSNLQ'><dt id='XSNLQ'><q id='XSNLQ'><span id='XSNLQ'><b id='XSNLQ'><form id='XSNLQ'><ins id='XSNLQ'></ins><ul id='XSNLQ'></ul><sub id='XSNLQ'></sub></form><legend id='XSNLQ'></legend><bdo id='XSNLQ'><pre id='XSNLQ'><center id='XSNLQ'></center></pre></bdo></b><th id='XSNLQ'></th></span></q></dt></tr></i><div id='XSNLQ'><tfoot id='XSNLQ'></tfoot><dl id='XSNLQ'><fieldset id='XSNLQ'></fieldset></dl></div>
        <bdo id='XSNLQ'></bdo><ul id='XSNLQ'></ul>
    2. MySQL:如果表中不存在则插入记录

      时间:2023-08-20
          <tbody id='FVjsN'></tbody>

      • <small id='FVjsN'></small><noframes id='FVjsN'>

        <tfoot id='FVjsN'></tfoot>
        <legend id='FVjsN'><style id='FVjsN'><dir id='FVjsN'><q id='FVjsN'></q></dir></style></legend>

          1. <i id='FVjsN'><tr id='FVjsN'><dt id='FVjsN'><q id='FVjsN'><span id='FVjsN'><b id='FVjsN'><form id='FVjsN'><ins id='FVjsN'></ins><ul id='FVjsN'></ul><sub id='FVjsN'></sub></form><legend id='FVjsN'></legend><bdo id='FVjsN'><pre id='FVjsN'><center id='FVjsN'></center></pre></bdo></b><th id='FVjsN'></th></span></q></dt></tr></i><div id='FVjsN'><tfoot id='FVjsN'></tfoot><dl id='FVjsN'><fieldset id='FVjsN'></fieldset></dl></div>

                <bdo id='FVjsN'></bdo><ul id='FVjsN'></ul>
                本文介绍了MySQL:如果表中不存在则插入记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试执行以下查询:

                I am trying to execute the following query:

                INSERT INTO table_listnames (name, address, tele)
                VALUES ('Rupert', 'Somewhere', '022')
                WHERE NOT EXISTS (
                    SELECT name FROM table_listnames WHERE name='value'
                );
                

                但这会返回错误.基本上,如果记录的名称"字段已存在于另一条记录中,我不想插入记录 - 如何检查新名称是否唯一?

                But this returns an error. Basically I don't want to insert a record if the 'name' field of the record already exists in another record - how to check if the new name is unique?

                推荐答案

                我实际上并不是建议您这样做,因为 UNIQUE 索引/stackoverflow.com/a/3164595/5411817">Piskvor 和其他人是一种更好的方法,但您实际上可以做您正在尝试的事情:

                I'm not actually suggesting that you do this, as the UNIQUE index as suggested by Piskvor and others is a far better way to do it, but you can actually do what you were attempting:

                CREATE TABLE `table_listnames` (
                  `id` int(11) NOT NULL auto_increment,
                  `name` varchar(255) NOT NULL,
                  `address` varchar(255) NOT NULL,
                  `tele` varchar(255) NOT NULL,
                  PRIMARY KEY  (`id`)
                ) ENGINE=InnoDB;
                

                插入记录:

                INSERT INTO table_listnames (name, address, tele)
                SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp
                WHERE NOT EXISTS (
                    SELECT name FROM table_listnames WHERE name = 'Rupert'
                ) LIMIT 1;
                
                Query OK, 1 row affected (0.00 sec)
                Records: 1  Duplicates: 0  Warnings: 0
                
                SELECT * FROM `table_listnames`;
                
                +----+--------+-----------+------+
                | id | name   | address   | tele |
                +----+--------+-----------+------+
                |  1 | Rupert | Somewhere | 022  |
                +----+--------+-----------+------+
                

                尝试再次插入相同的记录:

                Try to insert the same record again:

                INSERT INTO table_listnames (name, address, tele)
                SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp
                WHERE NOT EXISTS (
                    SELECT name FROM table_listnames WHERE name = 'Rupert'
                ) LIMIT 1;
                
                Query OK, 0 rows affected (0.00 sec)
                Records: 0  Duplicates: 0  Warnings: 0
                
                +----+--------+-----------+------+
                | id | name   | address   | tele |
                +----+--------+-----------+------+
                |  1 | Rupert | Somewhere | 022  |
                +----+--------+-----------+------+
                

                插入不同的记录:

                INSERT INTO table_listnames (name, address, tele)
                SELECT * FROM (SELECT 'John', 'Doe', '022') AS tmp
                WHERE NOT EXISTS (
                    SELECT name FROM table_listnames WHERE name = 'John'
                ) LIMIT 1;
                
                Query OK, 1 row affected (0.00 sec)
                Records: 1  Duplicates: 0  Warnings: 0
                
                SELECT * FROM `table_listnames`;
                
                +----+--------+-----------+------+
                | id | name   | address   | tele |
                +----+--------+-----------+------+
                |  1 | Rupert | Somewhere | 022  |
                |  2 | John   | Doe       | 022  |
                +----+--------+-----------+------+
                

                等等……

                更新:

                为了防止 #1060 - Duplicate column name 错误,以防两个值可能相等,您必须命名内部 SELECT 的列:

                To prevent #1060 - Duplicate column name error in case two values may equal, you must name the columns of the inner SELECT:

                INSERT INTO table_listnames (name, address, tele)
                SELECT * FROM (SELECT 'Unknown' AS name, 'Unknown' AS address, '022' AS tele) AS tmp
                WHERE NOT EXISTS (
                    SELECT name FROM table_listnames WHERE name = 'Rupert'
                ) LIMIT 1;
                
                Query OK, 1 row affected (0.00 sec)
                Records: 1  Duplicates: 0  Warnings: 0
                
                SELECT * FROM `table_listnames`;
                
                +----+---------+-----------+------+
                | id | name    | address   | tele |
                +----+---------+-----------+------+
                |  1 | Rupert  | Somewhere | 022  |
                |  2 | John    | Doe       | 022  |
                |  3 | Unknown | Unknown   | 022  |
                +----+---------+-----------+------+
                

                这篇关于MySQL:如果表中不存在则插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:您不能在 FROM 子句中指定要更新的目标表 下一篇:MySQL 错误 #1071 - 指定的键太长;最大密钥长度为

                相关文章

                最新文章

                    <bdo id='NdgvO'></bdo><ul id='NdgvO'></ul>
                1. <i id='NdgvO'><tr id='NdgvO'><dt id='NdgvO'><q id='NdgvO'><span id='NdgvO'><b id='NdgvO'><form id='NdgvO'><ins id='NdgvO'></ins><ul id='NdgvO'></ul><sub id='NdgvO'></sub></form><legend id='NdgvO'></legend><bdo id='NdgvO'><pre id='NdgvO'><center id='NdgvO'></center></pre></bdo></b><th id='NdgvO'></th></span></q></dt></tr></i><div id='NdgvO'><tfoot id='NdgvO'></tfoot><dl id='NdgvO'><fieldset id='NdgvO'></fieldset></dl></div>

                2. <tfoot id='NdgvO'></tfoot>
                3. <small id='NdgvO'></small><noframes id='NdgvO'>

                  <legend id='NdgvO'><style id='NdgvO'><dir id='NdgvO'><q id='NdgvO'></q></dir></style></legend>