使用一个单元格引用另一个数据库并将两者组合在查询中

发布于 2024-12-28 18:34:51 字数 209 浏览 4 评论 0原文

我试图弄清楚是否有一种方法可以获取单元格中的值并将其替换为该数字在另一个表中引用的用户的文本名称。

我已经查看了子查询,但我认为这不是我想要的。例如,我的查询现在返回为用户 ID、州、国家/地区。还有另一个表有 userID、name。我想查询第一个数据库,但将 userID 替换为另一个表中相应的名称。

这可行吗?我正在使用 mySQL Workbench 进行查询。

I am trying to figure out if there is a way to take the value in a cell and replace it with the text name of a user that that number refers to in another table.

I have looked through subqueries but I don't think that is what I want. So for example my query comes back now as userID, state, country. And there is another table that has userID, name. I want to query the first database but to have userID replaced with its corresponding name from the other table.

Is that doable? I am using mySQL Workbench to make my queries.

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

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

发布评论

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

评论(2

空心空情空意 2025-01-04 18:34:52

如果我明白你需要什么,你可以尝试这个(备份后!!)

UPDATE table1 SET userID = 
    (SELECT name FROM table2
     WHERE table2.userID = table1.userID)

如果你只需要从两个表中获取结果,你可以使用

SELECT t2.name, t1.state, t1.country
FROM table1 t1 INNER JOIN table2 t2
ON t1.userID = t2.userID

If I understand what you need you could try this (after a backup!!)

UPDATE table1 SET userID = 
    (SELECT name FROM table2
     WHERE table2.userID = table1.userID)

If you just need to get results from two tables you could use

SELECT t2.name, t1.state, t1.country
FROM table1 t1 INNER JOIN table2 t2
ON t1.userID = t2.userID
寒尘 2025-01-04 18:34:52

像这样?

select u.username, l.logintime, l.logouttime
from loginouttable l
join usertable u on l.userid = u.userid

Like this?

select u.username, l.logintime, l.logouttime
from loginouttable l
join usertable u on l.userid = u.userid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文