SQL:如何在更新语句中自引用列?

发布于 2024-12-17 10:20:49 字数 234 浏览 4 评论 0原文

我有两个包含这些列的表:

用户: 用户、班级、年级、位置。

课程: 类、位置

用户表的类列引用类表中的同一列。

我想更新用户表中的所有行,以便用户表的“位置”列的每一行都等于类的位置。

所以我有一行值: Mike,数学,A+,纽约

数学班级表中的相应行是: 数学,芝加哥

我希望用户表的行变成 迈克,数学,A+,芝加哥。

谢谢

I have two tables with these columns:

Users:
user, class, grade, location.

Classes:
class, location

The class column of the Users table references the same colmn in Classes table.

I want to update all rows in the Users table such that each row of the Users table's "location" column is equal to the location of the class.

So i have a row with values:
Mike, Math, A+, New York

And the corresponding row in Classes table for Math is:
Math, Chicago

I want the user table's row to become
Mike, Math, A+, Chicago.

Thanks

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

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

发布评论

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

评论(3

黯然#的苍凉 2024-12-24 10:20:49
update
  users
set
  users.location = classes.location
from
  classes
where
  classes.class = users.class
update
  users
set
  users.location = classes.location
from
  classes
where
  classes.class = users.class
葮薆情 2024-12-24 10:20:49

这个怎么样:

UPDATE U 
SET U.Location = C.Location
FROM Users AS U
INNER JOIN Classes AS C ON C.Class = U.Class
                AND C.Location != U.Location
 ;

How about this:

UPDATE U 
SET U.Location = C.Location
FROM Users AS U
INNER JOIN Classes AS C ON C.Class = U.Class
                AND C.Location != U.Location
 ;
素染倾城色 2024-12-24 10:20:49
update users a
    set a.location =
     (select b.location 
      from classes b
      where b.class = a.class) 
update users a
    set a.location =
     (select b.location 
      from classes b
      where b.class = a.class) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文