Home > Database > Mysql Tutorial > How to Insert Data with Selective Values and Defaults in MySQL?

How to Insert Data with Selective Values and Defaults in MySQL?

Patricia Arquette
Release: 2024-11-27 14:05:12
Original
856 people have browsed it

How to Insert Data with Selective Values and Defaults in MySQL?

Inserting into a Table with Selective Values and Defaults

Searching for a solution to insert data into one table while selecting specific values from another table and incorporating some default values can lead to challenges. You may encounter errors such as "mysql error" or "column count does not match."

To resolve this, consider the following structure for your INSERT statement:

INSERT INTO def (catid, title, page, publish) 
SELECT catid, title, 'page','yes' from `abc`
Copy after login

This updated query accomplishes the desired outcome by explicitly listing the values to be inserted into the def table. The catid and title values are selected from the abc table, while the page value is set to 'page' and the publish value is set to 'yes' as default values.

By adhering to the correct syntax, you can successfully insert data into your table while utilizing values from both the source table and default values.

The above is the detailed content of How to Insert Data with Selective Values and Defaults in MySQL?. 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