首页 > 解决方案 > 有没有办法加入 2 个表并验证它们拥有的数据

问题描述

我有 2 个复杂表,一个是主表,另一个是从主表派生的。我想将派生表加入主表并验证主表条目不能为空。

https://i.stack.imgur.com/Y3EuO.png

标签: sql-server

解决方案


你肯定需要重新审视你的桌子设计。

我不是很清楚你在找什么,但我觉得你在派生表中寻找一个或多个值为空的整体。对于这种情况,您可以编写如下查询。

   select distinct mt.mainid from maintable mt
    inner join derivedtable m on mt.mainid=m.id and m.name='MainId'
    inner join derivedtable u on mt.mainid=u.id and u.name='UserId'
    inner join derivedtable r on mt.mainid=r.id and r.name='RoleId'
    where (m.value is null or u.value is null or r.value is null)

推荐阅读