这有什么问题? 我的任务是下一步:我有一个XML文件,我需要显示所有城市都不是米克。 !城市并非独一无二。所以,例如,我有Mick-London,Tom-London,Charles-Paris。正确的答案只有巴黎。
查询适用于SQL Server 2008。 谢谢你的帮助。
这是XML:
declare @x xml
set @x = '<database>
<persons>
<person fio="Mick" id="1" />
<work city="London" size="450" />
<state>United Kingdom </state>
</persons>
<persons>
<person fio="Tom" id="8" />
<work city="London" size="500" />
<state>UK</state>
</persons>
<persons>
<person fio="Charles" id="9" />
<work city="Paris" size="450000" />
<state>Frace</state>
</persons>
<persons>
<person fio="Some_name1" id="10" />
<work city="Brussels" size="30000" />
<state>Belgium</state>
</persons>
<persons>
<person fio="Some_name2" id="11" />
<work city="Munich" size="30000" />
<state>Germany</state>
</persons>
</database>'
我目前的脚本是:
select @x.query('//work[not(//person[@fio="Mick" and @city = this/../work/@city])]');
答案 0 :(得分:1)
您可以使用该查询。它有一个内部查询,因为城市不是唯一的,所以它首先在内部查询中为Mick构建城市,然后在外部获取所有其他城市。
select
y.work.value('(@city)[1]', 'nvarchar(100)')
from @x.nodes('//persons/work') y(work)
where y.work.value('(@city)[1]', 'nvarchar(100)') not in
(select
x.work.value('(@city)[1]', 'nvarchar(100)')
from @x.nodes('//persons[person[@fio="Mick"]]/work') x(work))
根据要求,此查询仅使用query
:
select @x.query('//work[not(@city = (//persons[person[@fio="Mick"]]/work/@city))]');