English 中文(简体)
我如何用WHERE条款复制一个表格的数据?
原标题:How do I copy data from one table to another with WHERE clause?
  • 时间:2012-05-03 19:57:34
  •  标签:
  • mysql
  • sql

我有两张这样的表格。

TABLE1: id value number

TABLE2: id name value rid

我想将<代码>价值的从TABLE2转成TABLE1,在TABLE2.rid=TABLE1.id。 我已尝试如下:

INSERT INTO TABLE1 (value) SELECT value FROM TABLE2 WHERE TABLE2.rid=TABLE1.id.

然而,我之所以能够这样做,是因为我没有加入TABLE1和TABLE2——如果我试图这样做,我就犯了一起错误。 希望得到任何帮助。

问题回答

您需要一份与日本国际海洋研究网络条款一致的声明,而不是INSERT的声明,因为你已经掌握了表格中的数据,你希望在表格中填写一栏的数值。

Click here to view the demo inkou Fiddle.

<><>>:>

CREATE TABLE table1
(   id          INT NOT NULL 
    , value     INT NOT NULL
    , number    INT NOT NULL
);

CREATE TABLE table2
(       id      INT         NOT NULL
    ,   name    VARCHAR(30) NOT NULL
    ,   value   INT         NOT NULL
    ,   rid     INT         NOT NULL
);

INSERT INTO table1 (id, value, number) VALUES
  (1,  0, 111),
  (2,  0, 222),
  (3,  0, 333),
  (4, 10, 444);

INSERT INTO table2 (id, name, value, rid) VALUES
  (1,  abc , 123, 1),
  (2,  def , 345, 2),
  (3,  efg , 456, 3),
  (4,  ghi , 567, 4);

UPDATE      table1
INNER JOIN  table2
ON          table1.id       = table2.rid
SET         table1.value    = table2.value;

<><><><>>><>>>><>>>>>> • 管理:

table1:
ID  VALUE NUMBER
--  ----- ------
1     0    111
2     0    222
3     0    333
4    10    444

table2:
ID  NAME  VALUE RID
--  ----  ----- ---
1   abc   123   1
2   def   345   2
3   efg   456   3
4   ghi   567   4

<<><><>>>>>>><>>>>>>>> • 管理:

table1:
ID  VALUE NUMBER
--  ----- ------
1    123    111
2    345    222
3    456    333
4    567    444

table2:
ID  NAME  VALUE RID
--  ----  ----- ---
1   abc   123   1
2   def   345   2
3   efg   456   3
4   ghi   567   4
UPDATE table2 t2 
INNER JOIN table1 t1
    ON t2.rid=t1.id
SET t2.value = t1.value;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签