我在Patient表中有PatientUid主键,而且还有一些表,例如Patient Address,Contact Info,----我在其中使用它作为外键。我想在我的数据库中的表中搜索一个特定PatientUid的出现
答案 0 :(得分:0)
如果我的理解是正确的:
查找一个子表(PatientAddress)的No_of_Occurences
select PatientUID,count(PatientUID) as No_of_Occurences
from PatientAddress
group by PatientUID
同样适用于所有子表(ContactInfo)
然后使用UNION ALL操作符
来控制所有内容select PatientUID,count(PatientUID) as No_of_Occurences
from PatientAddress
group by PatientUID
union all
select PatientUID,count(PatientUID) as No_of_Occurences
from ContactInfo
group by PatientUID
然后,DB中所有表格的总No_of_Occurence可以在下面找到
select PatientUID,sum(No_of_Occurences) as No_of_Occurences
from
(
select PatientUID,count(PatientUID) as No_of_Occurences
from PatientAddress
group by PatientUID
union all
select PatientUID,count(PatientUID) as No_of_Occurences
from ContactInfo
group by PatientUID
) as all_tables
group by PatientUID
使用最终查询中的where子句搜索您选择的某个特定PatientUid的出现位置。
答案 1 :(得分:0)
选择 tab1.name为[referenced_table], col1.name as [referrenced_key_column], tab2.name为[primary_table], col2.name as [primary_Key_column] 来自sys.foreign_key_columns fkc
内部联接sys.objects obj on obj.object_id = fkc.constraint_object_id tab1.object_id = fkc.parent_object_id上的内连接sys.tables选项卡 col1.column_id = parent_column_id上的内连接sys.columns col1和col1.object_id = tab1.object_id tab2.object_id = fkc.referenced_object_id上的内连接sys.tables选项卡2 col2.column_id = referenced_column_id上的内连接sys.columns col2和col2.object_id = tab2.object_id
其中obj.name喜欢'%Patient%'和col2.name喜欢'%PatientUid%'