Home >Database >Mysql Tutorial >How to Insert Data with Default Values from One MySQL Table to Another?
Inserting Data from One Table and Default Values into Another
When attempting to insert data into a table by selecting from another table and adding default values, such as:
INSERT INTO def (catid, title, page, publish) (SELECT catid, title from abc),'page','yes')
you may encounter a MySQL error. This query will not work because the number of values provided in the subquery and the number of columns specified in the insert statement do not match. To resolve this issue and correctly insert the data, use the following syntax:
INSERT INTO def (catid, title, page, publish) SELECT catid, title, 'page','yes' from `abc`
In this modified query, we include the default values ('page' and 'yes') directly in the SELECT statement, ensuring that the number of values matches the number of columns in the destination table. This will allow the data to be inserted successfully.
The above is the detailed content of How to Insert Data with Default Values from One MySQL Table to Another?. For more information, please follow other related articles on the PHP Chinese website!