SQL-Join with NULL-columns
发布时间:2021-02-20 18:34:49 所属栏目:MsSql 来源:互联网
导读:我有以下表格: Table a+-------+------------------+------+-----+| Field | Type | Null | Key |+-------+------------------+------+-----+| bid | int(10) unsigned | YES | || cid | int(10
|
我有以下表格: Table a +-------+------------------+------+-----+ | Field | Type | Null | Key | +-------+------------------+------+-----+ | bid | int(10) unsigned | YES | | | cid | int(10) unsigned | YES | | +-------+------------------+------+-----+ Table b +-------+------------------+------+ | Field | Type | Null | +-------+------------------+------+ | bid | int(10) unsigned | NO | | cid | int(10) unsigned | NO | | data | int(10) unsigned | NO | +-------+------------------+------+ 当我想从b中选择a中相应的bid / cid-pair的所有行时,我只使用自然连接SELECT b.* FROM b NATURAL JOIN a;一切都很好. 当a.bid或a.cid为NULL时,我想获得另一列匹配的每一行,例如如果a.bid是NULL,我想要a.cid = b.cid的每一行,如果两者都是NULL我想要b中的每一列. 我天真的解决方案是这样的: SELECT DISTINCT b.* FROM b JOIN a ON ( ISNULL(a.bid) OR a.bid=b.bid ) AND (ISNULL(a.cid) OR a.cid=b.cid ) 有没有更好的方法来做到这一点? 解决方法不,这就是它.(我通常将ISNULL(a.bind)改为a.bind IS NULL,用于ANSI SQL合规性FWIW.) (编辑:吉安站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- sql – 有人可以解释一下
- Transactional replication(事务复制)详解之如何跳过一个
- CentOS Linux系统下安装Redis过程和配置参数说明
- sql-server – SQL Server中EXPLAIN表单SQLite的等价物是什
- SQL Server自定义异常raiserror使用示例
- 在telnet下操作memcache详解(操作命令详解)
- sql-server – SQL Server 2014备份到2012年
- sql-server – 在启动脚本/配置文件中存储数据库密码的最佳
- sql – 向数据库中的所有表添加位掩码是否有用?
- SqlServer使用公用表表达式(CTE)实现无限级树形构建
