Home  >  Article  >  Database  >  MySQL插入数据_MySQL

MySQL插入数据_MySQL

WBOY
WBOYOriginal
2016-06-01 13:01:59872browse

插入数据使用 INSERT

插入完整的行插入行的一部分插入多行插入某些查询的结果

插入完整的行

先看一下原有的customer列表: \

插入一行: INSERT INTO customers VALUES(NULL, 'Pep E. LaPew', '100 Main Street', 'Los Angeles', 'CA', '90046', 'USA', NULL, NULL);
\

还有一种方式,需要指定列名,这种方法,在表的结构发生变化时,其SQL语句仍然可以使用,而且这种赋值不需要与表的原有结构相同。 \

插入多行

插入多行可以将多个INSERT语句并列起来:
INSERT ..... VALUES(,,,,);
INSERT ..... VALUES(,,,,); 这样就可以插入两条记录了,另外如果要插入的列名相同,则可以如下合并插入语句:
INSERT INTO NAME(,,,,,,,) //后续插入的多条记录用到相同的列名 VALUES(,,,,,,,,,) , (.........), (.........); //这样就插入了三条记录了。

插入检索出的数据

其实就是使用SELECT语句检索出数据,作为VALUES的值来插入到表中,很好理解,下面的SQL语句就是将custnew表合并到customer表中:
INSERT INTO customers(cust_id, cust_contact, cust_email, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country) SELECT cust_id, cust_contact, cust_email, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country FROM custnew;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn