外连接或动态查询,哪个是最好的方法?
我有下表(我简化了表中包含的数据)。
RateTable
- rate_table_id [int]
- rate_table_name [nvarchar(50)]
RateTableUsed
- rate_used_id [int]
- rate_table_id [int] (key to RateTable.rate_table_id)
- customer_id [int]
RateTableExtra
- rate_table_extra_id [int]
- rate_ extra_id [int] (key to RateExtra.rate_table_id)
- rate_used_id [int] (key to RateTableUsed.rate_used_id)
RateExtra
- rate_ extra_id [int]
- rate_extra_name [nvarchar(50)]
select rate_table_name, rate_table_extra_id, rate_extra_name from RateTableUsed rtu
innerjoin RateTable rt on rtu.rate_table_id = rt.rate_table_id
innerjoin RateTableExtra rte on rte.rate_table_id = rt.rate_table_id
innerjoing RateExtr re on rte.rate_extra_id = re.rate_extra_id
RateExtra 仅包含 3 个值,键为 1、2 和 1。 3、汽油附加费、管理费和消费税。
这对于当前的目的来说工作得很好。 正在显示包含匹配记录的值列表,并且仅针对rate_extra_name 查询RateExtra。
因此,我可能有以下结果:
- Ratetable1, 1, PetrolSurcharge
- Ratetable1, 2, ManagementFee
- Ratetable2, 3, PetrolSurcharge
- Ratetable4, 4, GST
- Ratetable6, 5, PetrolSurcharge
我被要求修改此内容,以便现在返回的每条记录都包含记录对于 RateExtra 表中的每个值。 如果没有匹配的记录,那么我的 RateTableExtra 表中的数据应返回为 NULL。 所以我的数据应该返回为:
- Ratetable1, 1, PetrolSurcharge
- Ratetable1, 2, ManagementFee
- Ratetable1, NULL, GST
- Ratetable2, 3, PetrolSurcharge
- Ratetable2, NULL, ManagementFee
- Ratetable2, NULL, GST
- Ratetable4, NULL, PetrolSurcharge
- Ratetable4, NULL, ManagementFee
- Ratetable4, 4, GST
- Ratetable6, 5, PetrolSurcharge
- Ratetable6, NULL, ManagementFee
- Ratetable6, NULL, GST
我已经尝试过 OUTER 连接,但我假设它们似乎不起作用,因为 RateExtra 数据链接到 RateTableExtra ,它将返回 null 。 我现在正在考虑创建一个动态查询,该查询将获取我的原始结果集,对其进行迭代检查rate_extra_id,如果它不在结果集中,则在我需要的地方使用 NULL 数据将新行附加到结果中。 我假设这会起作用,但我有一种感觉,这将是性能的杀手。
有没有更好的方法来做到这一点? 希望有人能提供帮助,我们将不胜感激。
I have the following tables (I've simplified the data contained in the tables).
RateTable
- rate_table_id [int]
- rate_table_name [nvarchar(50)]
RateTableUsed
- rate_used_id [int]
- rate_table_id [int] (key to RateTable.rate_table_id)
- customer_id [int]
RateTableExtra
- rate_table_extra_id [int]
- rate_ extra_id [int] (key to RateExtra.rate_table_id)
- rate_used_id [int] (key to RateTableUsed.rate_used_id)
RateExtra
- rate_ extra_id [int]
- rate_extra_name [nvarchar(50)]
select rate_table_name, rate_table_extra_id, rate_extra_name from RateTableUsed rtu
innerjoin RateTable rt on rtu.rate_table_id = rt.rate_table_id
innerjoin RateTableExtra rte on rte.rate_table_id = rt.rate_table_id
innerjoing RateExtr re on rte.rate_extra_id = re.rate_extra_id
The RateExtra contains only 3 values with key's 1, 2 & 3 and names' petrol surcharge, management fee and GST.
This is working fine for it's current purpose. A list of values is being displayed with matching records and the RateExtra is queried only for the rate_extra_name.
So I may have the following results:
- Ratetable1, 1, PetrolSurcharge
- Ratetable1, 2, ManagementFee
- Ratetable2, 3, PetrolSurcharge
- Ratetable4, 4, GST
- Ratetable6, 5, PetrolSurcharge
I've been asked to modify this so that each record that returns now includes records for each value in the RateExtra table. If there are no matching records then data from my RateTableExtra table should come back as NULL. So my data should come back as:
- Ratetable1, 1, PetrolSurcharge
- Ratetable1, 2, ManagementFee
- Ratetable1, NULL, GST
- Ratetable2, 3, PetrolSurcharge
- Ratetable2, NULL, ManagementFee
- Ratetable2, NULL, GST
- Ratetable4, NULL, PetrolSurcharge
- Ratetable4, NULL, ManagementFee
- Ratetable4, 4, GST
- Ratetable6, 5, PetrolSurcharge
- Ratetable6, NULL, ManagementFee
- Ratetable6, NULL, GST
I've tried OUTER joins but they don't seem to be working I'm assuming because the RateExtra data is linked to the RateTableExtra which would return null. I'm now considering creating a dynamic query that will get my original result set, iterate over it checking for rate_extra_id and, if it's not already in the resultset, appending a new row to the results with NULL data where I need it. I'm assuming this would work but I've got a feeling it'd be a killer on performance.
Is there any better way to do this? Hope someone can help, it'd be really appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
您通过此查询得到的本质上是
RateTable
和RateExtra
之间的笛卡尔联接,并使用left join
来查找与这些对匹配的RateTableExtra
值。 本质上,此查询返回RateTable
和RateExtra
的所有可能组合,并显示RateTableExtra< 中包含哪些组合。 /code> 快速表。
小心笛卡尔连接。 如果桌子大小合理,他们很快就会失控!
享受!
Try this:
What you're getting with this query is, essentially, a Cartesian Join between
RateTable
andRateExtra
, with aleft join
to find the values ofRateTableExtra
that match those pairs. Essentially, this query is bringing back all possible combinations ofRateTable
andRateExtra
, and showing you which ones you have in yourRateTableExtra
table quickly.Be careful with Cartesian Joins. They can get out of hand very quickly with reasonably sized tables!
Enjoy!