将表 1 中的 2 列连接到表 2
您如何将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜想您正在 PHP 或其他东西中检索这些行,并且您正在获取哈希数组中的行,并以字段名称为键。 当然,哈希中只能有一个元素具有给定的键。 因此,您需要使用列别名来确保具有相同名称的列被赋予不同的别名。
现在你的哈希数组将有键“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.
Now your hash-array will have keys "wstate" and "cstate". Without aliasing these columns, one will always overwrite the other.
您从结果中得到了什么结论?
对于初学者来说,这将会令人困惑,因为两个联接中的字段名称以及主表中的一些字段名称是相同的。 显式选择输出列并为它们提供有意义的别名是一个非常好的主意。
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.
怎么样:
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"
谢谢比尔,我也添加了 StateName
SELECT w.*,
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.*,
FROM weddings AS w
INNER JOIN states AS s1 ON w.WeddingState = s1.StateId
INNER JOIN states AS s2 ON w.ContactState = s2.StateId