kerry
kerry

Reputation: 691

Create new table from old table and add 2 new columns

I have a query that is working fine, it creates one MySQL table from another based on selected fields.

My problem is I also want 2 new empty columns in the new table.

How do I create a new table based on an existing table, but with added empty columns?

$result = mysqli_query($mysqli,"CREATE TABLE matchstats SELECT hometeam, fthg, awayteam, ftag FROM results");
if (!$result) {
    die("Query to create table failed");
}

Upvotes: 0

Views: 261

Answers (2)

Amani Ben Azzouz
Amani Ben Azzouz

Reputation: 2565

You can try this

CREATE TABLE table2 (
  new_column1 DECIMAL(5,2),
  new_column2 DECIMAL(5,2)
) SELECT * FROM table1 ;

see demo

Upvotes: 3

Himanshu Patel
Himanshu Patel

Reputation: 243

First You can create new table from existing table. Below query is for that

create table `matchstats` AS Select * from `results`

Then you can add whatever columns you want into new table. Below query for that

ALTER TABLE `matchstats`
ADD DateOfBirth  date

Upvotes: 0

Related Questions