带有负数的sql连接问题
我需要使用联接从两个表中选择数据。 这相当简单,在这里没有问题。 当我要加入的字段用作两个单独的外键(我没有设计这个)时,就会出现问题。 所以我加入的 ID 字段要么是正数,要么是负数。
如果是正数,则与 table_2 表中的 ID_1 相关;如果是负数,则与 table_2 表中的 ID_2 相关。 然而,ID_2 将是一个正数(即使它在外键中存储为负数)。 显然,没有任何约束来强制执行这些 - 所以本质上不是真正的外键:/
我使用的 SQL 是这样的,并且对于正数来说很好:
select t1.Stuff, t2.MoreStuff from table_1 t1
join table_2 t2 on t1.ID_1 = t2.ID_1
where ...
如何将其负数合并到连接中。 这可能吗? 理想情况下,我想根据我的需要更改表格,但显然这不是一个有效的选择。 我真的被困住了。
我唯一的其他想法是一个单独的 sql 语句来处理这些奇怪的语句。 这一切都是由 C# 中的 clr sql 运行的。 在代码中添加一个单独的 SqlCommand 很可能会减慢速度,因此我宁愿将其全部保留在一个命令中。
欢迎您的意见,谢谢:)
I need to select data from two table using a join. This is fairly simple and have no problems here. The problem occurs when the field I am joining is used as two separate foreign keys (I didn't design this). So the ID field that I join on is either a positive or negative number.
If it's a positive number it relates to ID_1 on the table_2 table, if it's a negative, the number relates to ID_2 on the table_2 table. However the ID_2 will be a positive number (even though it's stored as a negative in the foreign key). Obviously there are no constraints to enforce these - so in essence not real foreign keys :/
The SQL I'm using goes something like this and is fine for the positive numbers:
select t1.Stuff, t2.MoreStuff from table_1 t1
join table_2 t2 on t1.ID_1 = t2.ID_1
where ...
How to incorporate the negative aspect of this into the join. Is this even possible? Ideally I'd like to alter the table to my needs but apparently this is not a valid option. I'm well and truly stuck.
The only other idea I've had is a separate sql statement to handle these odd ones. This is all being run by clr sql from C#. Adding a separate SqlCommand to the code will most likely slow things down hence why I'd prefer to keep it all in one command.
Your input is welcome, thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
假设表格如下所示:
...其中
fk
可用于使用id1
在Table2
中查找行(如果为正且 <) code>id2 如果为负。然后您可以按如下方式进行连接:
Let's say the tables look like this:
...where
fk
can be used to look up a row inTable2
usingid1
if positive andid2
if negative.Then you can do the join as follows:
最简单的方法 - 使用 UNION ALL 连接这些表:
Simpliest way - join these tables using UNION ALL:
这不会有很好的性能……但是,什么也不会。 您需要将否定键转换为肯定键,以及连接的条件逻辑。 像这样:
没有机会使用索引,因为您正在转换 t1.ID_1(使用 ABS 函数),但这是在给定情况下您可以做的最好的事情。
This won't be very performant...but then, nothing will. You need to transform your negative key into a positive one, and conditional logic for the join. Like this:
No chance of using an index, because you're transforming t1.ID_1 (with the ABS function), but it's the best that you can do given the circumstances.
您可以执行类似的操作,但前提是向架构设计者介绍 LART:
或者,
记住 LART,这是最重要的部分!
You can do something like this, but only after introducing the schema designer to a LART:
Alternatively,
Remember the LART, that's the most important part!
尝试这个
try this
它必须类似于
不确定我是否误解了你的问题。
It will have to be something like
Not sure if I have misunderstood your question.
通过在表二中应用左连接并使用绝对值函数,您应该能够完成您正在寻找的任务:
这里需要注意的是,如果
ID_1
和ID_2
不互斥,您将得到 2 个查询结果。By applying left joins across table two and using the absolute value function, you should be able to accomplish what you're looking for:
The caveat here is that if
ID_1
andID_2
are not mutually exclusive you will get 2 query results.