[英]Comparing two columns from different tables of different types in SQL
There is a column that exists in 2 tables. 2列中存在一列。 In table 1, this column contains values in binary form ( int
), 1
and 0
, while the other table contains the column in form 'Y'
and 'N'
. 在表1中,此列包含二进制形式( int
), 1
和0
,而另一个表包含'Y'
和'N'
形式的列。
Essentially I need to display rows in table 1 that contain values that are different from values in table 2 for that column. 基本上我需要在表1中显示包含与该列的表2中的值不同的值的行。 How do I compute 1 to Y and 0 to N for comparison? 如何计算1到Y和0到N进行比较?
Example: 例:
Table 1: 表格1:
DateRecorded SchoolName StudentName isAbsent hasPassed
------------ ---------- ----------- -------- ---------
2011-04-03 ABC John Y Y
2011-04-05 ABC John N Y
Table 2: 表2:
DateRecorded SchoolName StudentName isAbsent hasPassed
------------ ---------- ----------- -------- ---------
2011-04-03 ABC John 0 1
2011-04-05 ABC John 0 1
Should return row: 应该返回行:
2011-04-03 ABC John Y Y
from Table 1 as this row is conflicting with the same row in Table 2. 从表1可以看出,该行与表2中的同一行发生冲突。
Try this: 尝试这个:
SELECT * FROM tbl1
EXCEPT
SELECT
daterecorded,
schoolname,
studentname,
CASE isAbsent WHEN 1 THEN 'Y' WHEN 0 THEN 'N' END AS isAbsent,
CASE hasPassed WHEN 1 THEN 'Y' WHEN 0 THEN 'N' END AS hasPassed
FROM tbl2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.