Mysql更新数据

发布于 2024-11-07 21:23:03 字数 727 浏览 1 评论 0原文

更新

+-----+------------------+-------------+
| id  | datenum          | F1_baro_20_ |
+-----+------------------+-------------+
|   1 | 734152.000000000 |     1005.21 |
|   2 | 734152.006944445 |     1005.26 |
+-----+------------------+-------------+

这是我的表

这是我的新表

+-----+------------------+-------------+------------+
| id  | datenum          | F1_baro_20_ |new column  |
+-----+------------------+-------------+------------+
|   1 | 734152.000000000 |     1005.21 |            |
|   2 | 734152.006944445 |     1005.26 |            |
+-----+------------------+-------------+------------+

我想将数据插入到“new_colum”,其中 old_datenum=new_datenum 但我不想修改现有数据!

UPDATE

+-----+------------------+-------------+
| id  | datenum          | F1_baro_20_ |
+-----+------------------+-------------+
|   1 | 734152.000000000 |     1005.21 |
|   2 | 734152.006944445 |     1005.26 |
+-----+------------------+-------------+

this is my table

And this is my new table

+-----+------------------+-------------+------------+
| id  | datenum          | F1_baro_20_ |new column  |
+-----+------------------+-------------+------------+
|   1 | 734152.000000000 |     1005.21 |            |
|   2 | 734152.006944445 |     1005.26 |            |
+-----+------------------+-------------+------------+

I want to insert data to the 'new_colum' where old_datenum=new_datenum but I don't wan to modify the existing data!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人的歌 2024-11-14 21:23:03

试试这个。

使用以下 SQL 查询添加一个新列:

ALTER TABLE myTable ADD newColumn <datatype>

其中 可以是 int、text、longtext、varchar(50) 之类的内容 - 具体取决于您的需要。

然后使用这样的 SQL 查询仅更新此列:

UPDATE TABLE myTable SET newColumn = 'Hello this is my data'

或者如果您需要更新特定行(在列 id 为 2 的情况下选择):

UPDATE TABLE myTable SET newColumn = 'Hello this is my data' WHERE id = '2'

这是将数据从 datenum 复制到 newColumn 的简单 SQL 查询:

UPDATE myTable SET newColumn = datenum

运行该代码一次(但是当然还有你的表名和列名),你会看到数据被复制。


或者,如果您想使用 PHP 等来执行此操作:

$rs = mysql_query('SELECT * FROM myTable ORDER BY id ASC');
while ($row = mysql_fetch_array($rs)) {
  $id = $row['id'];
  $new_datenum = $row['datenum'];
  $q = mysql_query('UPDATE myTable SET newColumn = $new_datenum WHERE id = $id');
}

这也会将所有数据从 datenum 复制到 newColumn。

Try this.

Add a new column with the following SQL query:

ALTER TABLE myTable ADD newColumn <datatype>

Where <datatype> can be something like int, text, longtext, varchar(50) - depending on what you need.

Then update only this column with SQL queries like this:

UPDATE TABLE myTable SET newColumn = 'Hello this is my data'

or if you need to update a specific row (chosen where the column id is 2):

UPDATE TABLE myTable SET newColumn = 'Hello this is my data' WHERE id = '2'

Here's the simple SQL query to copy the data from datenum to newColumn:

UPDATE myTable SET newColumn = datenum

Run that code once (but with your table name and column name of course) and you'll see that the data is copied.


Or if you want to do it with, for example, PHP:

$rs = mysql_query('SELECT * FROM myTable ORDER BY id ASC');
while ($row = mysql_fetch_array($rs)) {
  $id = $row['id'];
  $new_datenum = $row['datenum'];
  $q = mysql_query('UPDATE myTable SET newColumn = $new_datenum WHERE id = $id');
}

This will also copy all the data from datenum to newColumn.

抱猫软卧 2024-11-14 21:23:03
update mytable set newcol = 'yourvalhere' where somecol='somevalue';

Where 子句是可选的,如果您想向整个表添加相同的值,请忽略它。有关更多信息,请发布您的架构示例以及您想要的数据。

另请参见

http://dev.mysql.com/doc/refman/5.0 /en/update.html

update mytable set newcol = 'yourvalhere' where somecol='somevalue';

The Where clause is optional, if you want to add the same value to the entire table ignore it. For more info post an example of your schema and what you want the data to be.

See also

http://dev.mysql.com/doc/refman/5.0/en/update.html

川水往事 2024-11-14 21:23:03

您可以简单地修改旧表来添加列。如果您不能这样做,那么只需在两个表上使用联接,如下所示

UPDATE newTable N INNER JOIN
oldTable O 
ON N.datenum = O.datenum
SET N.newcolumn = <whatever you want to set>
WHERE <any conditions on either tables> 

You can simply modify the old table to add the column.. If you cant do that, then just use a join on the two tables like this

UPDATE newTable N INNER JOIN
oldTable O 
ON N.datenum = O.datenum
SET N.newcolumn = <whatever you want to set>
WHERE <any conditions on either tables> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文