带有 2 个 ON 子句的 MYSQL 内连接
我正在尝试设置一个简单的数据库,其中有一个用户并存储他们的居住地址和邮政地址。我有 2 个表
用户
id (Primary Key)
name (Varchar 255)
residential_id (foreign key)
postal_id (foreign key)
地址
id (primary key)
type (enum of R and P)
street (varchar 255)
suburb (varchar 255)
我正在尝试进行内部联接,所以我最终得到一个看起来像的结果集。
id - name - Residential_street - housing_suburb、postal_street、postal_suburb
我不断收到地址详细信息的空结果,我认为这是因为我从地址表中获取两组数据并且存在冲突。是否可以同时返回与住宅 ID 和邮政 ID 关联的地址字段?
我的 SQL 语法是
SELECT * FROM users
LEFT JOIN address
ON (users.residential_id = address.id AND users.postal_id = address.id)
编辑。正如已经指出的那样,我的数据库设计相当糟糕,我正在寻求改进它。 我想要实现的关键是我可以存储一个人的详细信息以及他们相关的居住和邮政地址。我永远不会寻求扩展数据库以包含工作地址,因此希望能够降低表的复杂性。
I am trying to setup a simple database in which I have a user and store both their residential and postal address. I have 2 tables
Users
id (Primary Key)
name (Varchar 255)
residential_id (foreign key)
postal_id (foreign key)
Address
id (primary key)
type (enum of R and P)
street (varchar 255)
suburb (varchar 255)
I am tring to do an inner join so I end up with a result-set that looks like.
id - name - residential_street - residential_suburb, postal_street, postal_suburb
I keep getting null results for the address details, I assume this is because I am getting two sets of data from the address table and there is a conflict. Is it possible to return the address fields linked to the residential ID and the postal ID at the same time?
My SQL syntax is
SELECT * FROM users
LEFT JOIN address
ON (users.residential_id = address.id AND users.postal_id = address.id)
EDIT. As has been pointed out my DB design is rather poor and I am looking to improve it.
The key thing I am trying to achieve is that I can store the details of a person along with their associated residential and postal address. I will never be looking to expand the database to include a work address for example so hopefully that cuts down the complexity of the table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
下面假设
您想要的是:
这里的关键是您必须两次加入地址表。需要地址类型鉴别器,以便每个连接仅选择适当的地址类型。
如果您的架构不同,请澄清,我将修改我的答案。
The following assumes that
What you want is:
The key here is you have to join to the address table TWICE. The address type discriminator is needed so that each join selects only the appropriate type of address.
If your schema is different, please clarify and I'll modify my answer.
您正在检查列地址是否等于 id,我认为无论如何这都不是真的。
You're checking if the column address is equal to an id, I don't think this will be true in any case.
这相对容易。您只需加入地址表两次。
我个人并不喜欢这种数据模型。相反,我建议拥有一个地址类型字段并具有一对多关系(地址表中的 user.id 外键)。
您将面临的一个问题是确定用户对地址的所有权并不严格简单(在您的模型中)。查找孤立地址也不是。
一个建议:由于住宅和邮政是外键,因此尝试这样命名它们(例如住宅_id 和邮政_id),以便在阅读 SQL 时更清晰。
That's relatively easy. You just need to join to the address table twice.
This kind of data model is not one I personally favour. Instead I would suggest having an address type field and having a one-to-many relationship (user.id foreign key in address table).
One problem you'll face is that determining user ownership of addresses isn't strictly straightforward (in your model). Nor is finding orphaned addresses.
One suggestion: since residential and postal are foreign keys, try to name them as such (eg residential_id and postal_id) so it's clearer when reading the SQL.
@m3mbran3 我的建议是转储residential_id & 的 postal_id 字段
来自用户用户
地址
然后您又回到
@Cletus 建议的变体。请记住,如果用户有居住地址而没有邮政地址,反之亦然,则可能存在空值
@m3mbran3 my suggestion would be to dump the residential_id & postal_id fields from Users
Users
Address
Then you're back to
a variation of @Cletus suggestion. Remembering that if a user has a residential address and no postal address or vice/versa there may be nulls