-2

How to copy mysql data from table one to table two [php] ?

I want to copy data from table one to table two WHERE type=1 , How can I do ?

Table one

|  type  |  number  |   name   |  date  |
|    1   |     1    |     a    |   12   |
|    1   |     2    |     b    |   13   |
|    2   |     6    |     c    |   14   |
|    1   |     8    |     x    |   17   |
|    2   |     8    |     e    |   19   |
|    3   |     6    |     f    |   11   |
|    2   |     4    |     h    |   10   |
|    1   |     7    |     i    |   11   |
|    1   |     9    |     p    |   13   |
|    2   |     5    |     r    |   17   |
|    1   |     3    |     t    |   12   |

table two (out put)

|  number  |   name   |
|     1    |     a    |
|     2    |     b    |
|     8    |     x    |
|     7    |     i    |
|     9    |     p    |
|     3    |     t    |

2 Answers2

0

This is the basic standard query for the MySQL.

INSERT INTO TARGET_TABLE SELECT * FROM SOURCE_TABLE WHERE Type = 1

Using MySQLi in PHP you can do like this..

$mysqli->query("INSERT INTO `TARGET_TABLE` SELECT * FROM `SOURCE_TABLE` WHERE `Type` = 1")
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

You can do this with one SQL statement:

insert into two(number, name)
    select number, name
    from one
    where type = 1;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786