将表 1 中的 2 列连接到表 2

发布于 2024-07-12 05:18:12 字数 489 浏览 6 评论 0原文

您如何将 table1 列引用到表 2 中的 2 列

我创建了一个表“State”,其中包含 50 个精确行,

试图在“Wedding”表中关联 (weddingState,contactState)

这是我创建的语句,但它只正确连接顶部 WeddingState - 似乎不关心它下面的 INNER Join...

SELECT *

FROM weddings

INNER JOIN 状态为 s1 ON weddings。WeddingState = s1.StateId //婚姻状态

INNER JOIN 状态为 s2 ON weddings.ContactState = s2.StateId //新娘的联系状态

WHERE weddings.weddingid="094829292"

How would you reference table1 columns to 2 columns in table 2

I created a table 'State' with 50 exact rows

trying to relate (weddingState,contactState) in 'Wedding' table

This is the statement that I created but it only joins the top WeddingState correctly - seems not to care about the INNER Join below it...

SELECT *

FROM weddings

INNER JOIN states as s1 ON weddings.WeddingState = s1.StateId //state of marriage

INNER JOIN states as s2 ON weddings.ContactState = s2.StateId //contact state of bride

WHERE weddings.weddingid="094829292"

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

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

发布评论

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

评论(4

一念一轮回 2024-07-19 05:18:12

我猜想您正在 PHP 或其他东西中检索这些行,并且您正在获取哈希数组中的行,并以字段名称为键。 当然,哈希中只能有一个元素具有给定的键。 因此,您需要使用列别名来确保具有相同名称的列被赋予不同的别名。

SELECT w.*, s1.StateID AS wstate, s2.StateId AS cstate
FROM weddings AS w
INNER JOIN states AS s1 ON w.WeddingState = s1.StateId //state of marriage
INNER JOIN states AS s2 ON w.ContactState = s2.StateId //contact state of bride
WHERE w.weddingid="094829292";

现在你的哈希数组将有键“wstate”和“cstate”。 如果不对这些列设置别名,其中一个列将始终覆盖另一列。

I'd guess that you're retrieving these in PHP or something, and you're fetching the rows in a hash-array, keyed by the field name. Of course there can only be one element in a hash with a given key. So you need to use column aliases to make sure columns with the same name are given a distinct alias.

SELECT w.*, s1.StateID AS wstate, s2.StateId AS cstate
FROM weddings AS w
INNER JOIN states AS s1 ON w.WeddingState = s1.StateId //state of marriage
INNER JOIN states AS s2 ON w.ContactState = s2.StateId //contact state of bride
WHERE w.weddingid="094829292";

Now your hash-array will have keys "wstate" and "cstate". Without aliasing these columns, one will always overwrite the other.

迷乱花海 2024-07-19 05:18:12

您从结果中得到了什么结论?

对于初学者来说,这将会令人困惑,因为两个联接中的字段名称以及主表中的一些字段名称是相同的。 显式选择输出列并为它们提供有意义的别名是一个非常好的主意。

And what are you getting for your result that leads you to your conclusion?

It's going to be confusing for starters because the field names in the two joins, plus some of the field names in the primary table, are identical. It's a really good idea to explicitly choose your output columns and give them meaningful aliases.

寂寞花火° 2024-07-19 05:18:12

怎么样:

SELECT s1.StateName, s2.StateName

FROM weddings

INNER JOIN states as s1 ON weddings.WeddingState = s1.StateId //婚姻状态

INNER JOIN states as s2 ON weddings.ContactState = s2.StateId //新娘的联系状态

哪里 weddings.weddingid="094829292"

How about:

SELECT s1.StateName, s2.StateName

FROM weddings

INNER JOIN states as s1 ON weddings.WeddingState = s1.StateId //state of marriage

INNER JOIN states as s2 ON weddings.ContactState = s2.StateId //contact state of bride

WHERE weddings.weddingid="094829292"

硬不硬你别怂 2024-07-19 05:18:12

谢谢比尔,我也添加了 StateName

SELECT w.*,

s1.StateId AS WeddingStateId,

s1.StateName AS WeddingStateName,

s2.StateId AS ContactStateId,

s2.StateName AS ContactStateName

FROM weddings AS w

INNER JOIN states AS s1 ON w.WeddingState = s1.StateId

INNER JOIN states AS s2 ON w.ContactState = s2.StateId

Thanks Bill, I added the StateName as well

SELECT w.*,

s1.StateId AS WeddingStateId,

s1.StateName AS WeddingStateName,

s2.StateId AS ContactStateId,

s2.StateName AS ContactStateName

FROM weddings AS w

INNER JOIN states AS s1 ON w.WeddingState = s1.StateId

INNER JOIN states AS s2 ON w.ContactState = s2.StateId

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