c# - 检查并比较 sql server 表中的列值

标签 c# sql sql-server database join

我有这张表 (Prefrences_Table)

--------------------------
|student | Preferences |
--------------------------
Stud A   |  Stud B  
Stud A   |  Stud C
Stud B   |  Stud E
Stud B   |  Stud A
Stud C   |  Stud F
Stud F   |  Stud B
--------------------------

如果“Stud A”在他的偏好列表中添加了“Stud B”,我想检查“stud B”是否也在他的偏好列表中添加了“stud A”,所以我可以将他们都添加到一个组中. 如何使用 SQL 或 C# 完成此操作?

最佳答案

自连接在这里应该工作得很好。附加谓词仅返回匹配项的第一个实例以避免重复。

select t.student, t1.student
from 
  Prefrences_Table t
  inner join Prefrences_Table t1
    on t.student = t1.preferences
       and t.preferences = t1.student
       and t.student < t1.student

关于c# - 检查并比较 sql server 表中的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19185363/

相关文章:

java - 用于大型数据集的嵌入式 Java 数据库

c# - 是否有更好的方法在 CLR 中验证 SqlXml?

mysql - 将 SQL Server 数据库架构移植到 MySQL 的最佳方式

c# - DataTable.Load() 时应用程序中断,问题在哪里?

sql - 如何在mysql中创建计算字段?

c# - 不等待消息框的结果

mysql - SQL - 获取当前日期的数据

SQL - 删除从第一行开始包含 3.72 亿行的表

c# - Azure AD 通过补丁调用更改密码

c# - 在当前打开的应用程序顶部显示隐藏的窗口窗体