Home > Database > Mysql Tutorial > How to Insert Data with Default Values from One MySQL Table to Another?

How to Insert Data with Default Values from One MySQL Table to Another?

Barbara Streisand
Release: 2024-12-06 22:14:13
Original
209 people have browsed it

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')
Copy after login

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`
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template