如何比较 SQL Server 中两列的相等性? [英] How do I compare two columns for equality in SQL Server?

查看:51
本文介绍了如何比较 SQL Server 中两列的相等性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两列根据某些条件连接在一起,但我还想检查其他两列是否相同,如果相同则返回一个位字段.

I have two columns that are joined together on certain criteria, but I would also like to check if two other columns are identical and then return a bit field if they are.

有没有比使用 CASE WHEN 更简单的解决方案?

Is there a simpler solution than using CASE WHEN?

理想情况下,我可以使用:

Ideally I could just use:

    SELECT Column1 = Column2 AS MyDesiredResult
      FROM Table1
INNER JOIN Table2 ON Table1.PrimaryKey = Table2.ForeignKey

推荐答案

CASE 有什么问题?为了查看结果,您至少需要一个字节,这就是您使用单个字符获得的结果.

What's wrong with CASE for this? In order to see the result, you'll need at least a byte, and that's what you get with a single character.

CASE WHEN COLUMN1 = COLUMN2 THEN '1' ELSE '0' END AS MyDesiredResult

应该可以正常工作,并且就所有意图和目的而言,与使用位域的功能相同.

should work fine, and for all intents and purposes accomplishes the same thing as using a bit field.

这篇关于如何比较 SQL Server 中两列的相等性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆