有没有一种方法可以联接2个表并验证它们具有的数据

时间:2019-01-31 14:08:09

标签: sql-server

我有2个复杂表,一个是主表,另一个是从主表派生的。我想将派生表加入主表,并验证主表项,其中主表项不能为null。

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

1 个答案:

答案 0 :(得分:0)

您需要确定重新设计表设计。

我不太清楚您要查找的内容,但是我觉得您正在寻找派生表中一个或多个值为空的整数。对于这种情况,您可以编写如下查询。

   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)
相关问题