sql select 语句请教,一个表中的列1、列2对比出不同的数据。 sql select 语句请教,一个表中的列1、列2对比出不...

sql select \u8bed\u53e5\u8bf7\u6559,\u4e00\u4e2a\u8868\u4e2d\u7684\u52171\u3001\u52172\u5bf9\u6bd4\u51fa\u4e0d\u540c\u7684\u6570\u636e?????

\u95ee\u9898\u4e2d\u52172\u6709\u7a7a\u503c\uff0c\u7a7a\u503c\u4e0d\u80fd\u76f4\u63a5\u53c2\u4e0e\u6bd4\u8f83\uff0c\u6240\u4ee5\u6bd4\u8f83\u7684\u65f6\u5019\u5148\u8f6c\u6362\u6210\u4e00\u4e2a\u7279\u6b8a\u7684\u503c\uff0c\u7136\u540e\u518d\u8fdb\u884c\u6bd4\u8f83\u3002\u4e0b\u5217\u8bed\u53e5\u662f\uff0c\u5f53\u52172\u7684\u662f\u7a7a\u503c\u65f6\uff0c\u8f6c\u6362\u6210\u4e00\u4e2a\u7a7a\u683c\u3002
\u67e5\u8be2\u52171\u4e2d\u7684\u503c\u4e0d\u540c\u4e8e\u52172\u503c\u7684\u8bb0\u5f55\uff1a
select * from \u8868 where \u52171 not in (select nvl(\u52172,\u2018 \u2019) from \u8868);
\u67e5\u8be2\u52172\u4e2d\u7684\u503c\u4e0d\u540c\u4e8e\u52171\u503c\u7684\u8bb0\u5f55\uff1a
select * from \u8868 where nvl(\u52172,\u2018 \u2019) not in (select \u52171 from \u8868);

select * from \u8868\u683c\u540d where \u52171 \u52172;

问题中列2有空值,空值不能直接参与比较,所以比较的时候先转换成一个特殊的值,然后再进行比较。下列语句是,当列2的是空值时,转换成一个空格。
查询列1中的值不同于列2值的记录:
select * from 表 where 列1 not in (select nvl(列2,‘ ’) from 表);
查询列2中的值不同于列1值的记录:
select * from 表 where nvl(列2,‘ ’) not in (select 列1 from 表);

如果是在一张表中,为啥不这么写呢:
T/SQL:select 列1,列2 from 表 where isnull(列1,'')<>isnull(列2,'')
PL/SQL:select 列1,列2 from 表 where NVL(列1,'')<>NVL(列2,'')

select * from 表 where c1<>c2 or c2 is null

select * from a
except
select * from b

select * from b
except
select * from a

扩展阅读:sql中select的用法 ... 在sql的select语句中 ... sql语句 select 赋值 ... sql多表联查select语句 ... sql语句select from ... sql select where ... sql select语句大全 ... select所有查询语句 ... select语句的执行结果 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网