【问题标题】:Update table from converted value in another table从另一个表中的转换值更新表
【发布时间】:2020-11-18 12:50:31
【问题描述】:

我正在尝试比较一个表中的 int 值和转换后的(varchar 到 INT)值,以在另一个表中找到一个值,然后通过 phpmyadmin 更新它。

评论表

id: 1   title:Lorem   postID: 15 

元表

ID:99   NewID: 123 Type: "older_id"   Value:"15"

SQL

update t1
set t1.postID = t2.NewID
from comment t1
inner join meta t2
on t1.postID = CAST(t2.value AS INT)
where t2.Type = "older_id";

我不确定我做错了什么,但我不断收到错误。

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。

标签: mysql sql phpmyadmin sql-update inner-join


【解决方案1】:

如果您使用的是 MySQL(这在 PHP 中很常见),那么正确的语法是:

update comment c join
       meta m
       on c.postId = (p2.value + 0)  -- this converts the value, although even this is not necessary
    set c.postID = m.NewID
    where m.Type = 'older_id';

MySQL 中没有from 子句。

【讨论】:

  • 请允许我同时为您提供达到 1M 的积分和金标签徽章...恭喜您取得了惊人的成就。
猜你喜欢
  • 2010-10-21
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 2021-10-16
  • 1970-01-01
相关资源
最近更新 更多