Creating a New Table from Multiple Existing Tables in MySQL
To create a new table that combines data and columns from multiple existing tables, you can use the JOIN operation. In MySQL, this can be achieved through the following steps:
SELECT * FROM people AS p JOIN details AS d ON p.id = d.person_id JOIN taxonomy AS t ON d.detail_id = t.id
SELECT p.id, p.last_name, p.first_name, p.email, d.content AS age FROM people AS p JOIN details AS d ON p.id = d.person_id JOIN taxonomy AS t ON d.detail_id = t.id WHERE t.taxonomy = 'age'
CREATE TABLE new_table AS SELECT p.id, p.last_name, p.first_name, p.email, d.content AS age FROM people AS p JOIN details AS d ON p.id = d.person_id JOIN taxonomy AS t ON d.detail_id = t.id WHERE t.taxonomy = 'age'
INSERT INTO new_table (id, last_name, first_name, email, age) SELECT p.id, p.last_name, p.first_name, p.email, d.content AS age FROM people AS p JOIN details AS d ON p.id = d.person_id JOIN taxonomy AS t ON d.detail_id = t.id WHERE t.taxonomy = 'age'
By using these methods, you can create new tables that combine data and columns from multiple existing tables in MySQL, providing a convenient and efficient way to organize and access information.
The above is the detailed content of How to Combine Data from Multiple MySQL Tables into a New Table?. For more information, please follow other related articles on the PHP Chinese website!