是否可以在一列上连接两个表 = 连接 2 列?
表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样做:
如果您的任一浮点数有小数点,则转换为
int
将失败。例如,这有效:
输出:
17
但这不起作用:
输出:
将 varchar 值 '1.37' 转换为数据类型 int。
You can do this:
If either of your floats have decimal points though, the conversion to
int
will fail.E.g., this works:
Output:
17
But this does not:
Output:
Conversion failed when converting the varchar value '1.37' to data type int.
听起来像是计算列的工作,那么它将是可索引的。
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
您可以在 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.