SQL将ID插入到特定id的另一个表中

时间:2015-06-06 07:29:00

标签: php mysql sql joomla

我有2张桌子。 我想将一些sql条目传输到另一个表。 13是表1中的catid,但在表2中我的catid是不同的。

table1                table2
catid  title alias    catid title  alias
13     abc   xyz       78    abc   xyz
13     test  test      78    test  test

3 个答案:

答案 0 :(得分:0)

您可以将其传递给静态,请执行以下操作: -

INSERT INTO table1 (catid, title, alias) SELECT '2', title, alias FROM table2

可选,如果您需要where子句

WHERE catid ='13'

答案 1 :(得分:-1)

Use This Sql Query:

For Getting the data from table1:

$sql = "SELECT * FROM table1 WHERE catid=13";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
      $title=$row["title"];
      $alias=$row["alias"];
    }
} else {
    echo "0 results";
}

For Inserting the data into table2:

update table table2 set title='$title', alias='$alias' where catid=78

答案 2 :(得分:-1)

Use This Sql Query:

For Getting the data from table1:

$sql = "SELECT * FROM table1 WHERE catid=13";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
      $catid=$row["catid"];
      $title=$row["title"];
      $alias=$row["alias"];
    }
} else {
    echo "0 results";
}

For Inserting the data into table2:

update table table2 set catid='$catid', title='$title', alias='$alias'  where catid=78