英文:
Find mismatch between two rows using SQL
问题
我有一个 SQL 查询,会返回两行数据,每行有 400 多列。我需要比较每一列,找出是否有不匹配的数据。
我在 Excel 中使用了以下公式 =IF(A2=A3,"TRUE","FALSE")
来做到这一点,并在第三行显示 TRUE 或 FALSE 的值。我该如何在 SQL 中实现相同的功能?
英文:
I have an sql query which will give two rows with 400+ columns. I have to compare each column and find if there are any mismatches.
I used Excel formla =IF(A2=A3,"TRUE","FALSE")
to do this and show TRUE or FALSE values in the third row. How do i do the same with SQL?
答案1
得分: 1
如果你的行来自同一张表:
从某个表中选择 * where cond1
减去(/除外)
从某个表中选择 * where cond2
cond1 是获取第一行的条件
cond2 是获取第二行的条件
在你的情况下,some_table 是 SQL 查询 -> 注意,在某些数据库中,你需要为嵌套查询指定一个名称
从(your_query)q 中选择 * where cond1
减去(/除外)
从(your_query)q 中选择 * where cond2
减去(/除外)-> 在某些数据库中,这个操作被称为减去(minus),而在其他数据库中被称为除外(except)
只有当行是不同的时候,你才会看到结果!
请注意,如果你想执行减去(除外)操作,你需要具有相同数量和数据类型的列
英文:
If your rows are from the same table:
select * from some_table where cond1
minus (/except)
select * from some_table where cond2
cond1 is where condition for fetching the first row
cond2 is where condition for fetching the second row
some_table in your case is sql query -> Note in some databases you need to give a name for that nested query
select * from (your_query) q where cond1
minus (/except)
select * from (your_query) q where cond2
minus (/except) -> in some databases this operation is called minus, and in the others is called except
You will see the result if rows are different!
Note that if you want to do minus(except) you need same number and datatype of columns
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论