如何联接一对多表,并将结果仅附加到较小的表?
基本上我有一个一对多的表。我想将较大表的列附加到较小表的结果集中。我最终将得到一个与较小表大小相同的结果集,但同样包含来自多面表的相关信息。
有一个问题:虽然可以建立复合键(同样它不作为约束),但多面表没有定义主键。
Basically I have a one-to-many table. I want to append the columns of the larger table to the result set of the smaller table. I will end up with a result set the same size of the smaller table, but again, with the relevant information from the many sided table.
One catch: the many sided table doesn't have a primary key defined, although a composite key could be established (again it isn't there as constraint).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
t_large
中的一条记录可能对应于t_small
中的一条记录,因此您需要选择要从t_large
中选取的内容。这意味着您需要聚合
t_large
中的所有相应记录(如何?),或者从多个记录中选择单个记录(哪个?)。或者
现在,想象一下这个表布局:
您能否发布您希望看到的查询结果的结果集?
Since more than one record in
t_large
may correspond to a record int_small
, you need to choose what exactly you want to pick fromt_large
.This means that you'll either need to aggregate all corresponding records from
t_large
(how?), or select a single record out of many (which?).or
Now, imagine this table layout:
Could you please post the resultset which you'd like to see as a result of the query?
这不就是键上的左连接吗?
select * fromsmall_table s left join large_table l on s.id = l.small_table_id
small_table 中的每条记录,但仅限 large_table 中的相关行。
我缺少一些东西。请详细说明或提供经过清理的样本数据。
Isn't this just an left join on the key?
select * from small_table s left join large_table l on s.id = l.small_table_id
Every record in small_table, but only the relevant rows in large_table.
I'm missing something. Please elaborate or provide scrubbed sample data.