是否可以在一列上连接两个表 = 连接 2 列?

发布于 2024-09-15 15:37:10 字数 246 浏览 5 评论 0原文

表 A 具有 X 列,它是由表 B 中的 Y 列和 Z 列(均为浮点数)串联而成的 int。我想以类似于此的方式连接表 A 和 B:

select *
from tableA a inner join tableB b
on a.X = b.cast(concat(cast(b.Y as varchar), cast(b.Z as varchar)) as integer

除了,显然,我的例子没有正确完成。

Table A has column X, which is an int made up of the concatenation of columns Y and Z (which are both floats) in table B. I want to join tables A and B in a manner similar to this:

select *
from tableA a inner join tableB b
on a.X = b.cast(concat(cast(b.Y as varchar), cast(b.Z as varchar)) as integer

Except that, obviously, my example is not correctly done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情栀口红 2024-09-22 15:37:10

您可以这样做:

select * 
from tableA a 
inner join tableB b 
on a.X = cast(cast(b.Y as varchar) + cast(b.Z as varchar) as int)

如果您的任一浮点数有小数点,则转换为 int 将失败。

例如,这有效:

declare @f1 as float
declare @f2 as float
set @f1 = 1
set @f2 = 7
select cast(cast(@f1 as varchar) + cast(@f2 as varchar) as int)

输出: 17

但这不起作用:

declare @f1 as float
declare @f2 as float
set @f1 = 1.3
set @f2 = 7
select cast(cast(@f1 as varchar) + cast(@f2 as varchar) as int)

输出: 将 varchar 值 '1.37' 转换为数据类型 int。

You can do this:

select * 
from tableA a 
inner join tableB b 
on a.X = cast(cast(b.Y as varchar) + cast(b.Z as varchar) as int)

If either of your floats have decimal points though, the conversion to int will fail.

E.g., this works:

declare @f1 as float
declare @f2 as float
set @f1 = 1
set @f2 = 7
select cast(cast(@f1 as varchar) + cast(@f2 as varchar) as int)

Output: 17

But this does not:

declare @f1 as float
declare @f2 as float
set @f1 = 1.3
set @f2 = 7
select cast(cast(@f1 as varchar) + cast(@f2 as varchar) as int)

Output: Conversion failed when converting the varchar value '1.37' to data type int.

深海里的那抹蓝 2024-09-22 15:37:10

听起来像是计算列的工作,那么它将是可索引的。

http://www.mssqltips.com/tip.asp?tip=1682

Sounds like a job for a computed column, then it would be index-able.

http://www.mssqltips.com/tip.asp?tip=1682

沫雨熙 2024-09-22 15:37:10

您可以在 b 中创建另一列名为 x 的列,其中包含您想要的值吗?
那么加入 A 就很容易了。

Can you create another column in b named x which contains the value you want?
Then the join to A is easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文