Home  >  Article  >  Database  >  How to Insert Data with Default Values from Another Table in MySQL?

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

DDD
DDDOriginal
2024-11-26 15:18:11995browse

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

Inserting Values from Another Table with Default Values in MySQL

You want to insert data into a table named def by selecting specific values from another table named abc. Additionally, you want to include some default values in the insertion.

The syntax for inserting values into a table while selecting from another table and including default values is as follows:

INSERT INTO target_table (column1, column2, ..., columnN)
SELECT column1, column2, ..., columnM, default_value1, default_value2, ... FROM source_table

In your case, you can insert data into the def table by executing the following query:

INSERT INTO def (catid, title, page, publish) 
SELECT catid, title, 'page', 'yes' FROM abc

Ensure that the number of columns in the SELECT clause matches the number of columns specified in the INSERT statement.

The above is the detailed content of How to Insert Data with Default Values from Another Table in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

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