使用xquery连接多个表

时间:2017-11-20 01:08:35

标签: xml join xquery

我正在努力寻找住在林肯并在奥马哈工作的员工的名字,奥马哈应该是3名员工,即{Brady,Dan L} {Clemson Ann M}和{Gill,Mary L}。但是我拥有的xQuery给了我多次出现的这些名字。这就是我到目前为止所做的:

for $e in doc("A8.xml")/empdata/contents/employee,
    $w in doc("A8.xml")/empdata/contents/works,
    $c in doc("A8.xml")/empdata/contents/company
where $e/city="Lincoln"
    and $c/city="Omaha"
    and $w/company_name=$c/company_name
return $e/employee_name

这是我的XML文件:

<?xml version="1.0"?>
<empdata>
    <contents>
        <employee>
            <employee_name>Anderson, Susan L</employee_name> 
            <street>108th</street> 
            <city>Omaha</city> 
            <gender>F</gender>
        </employee>
        <employee>
            <employee_name>Brady, Dan L</employee_name> 
            <street>P street</street> 
            <city>Lincoln</city> 
            <gender>M</gender>
        </employee>
        <employee>
            <employee_name>Chen, Peter K</employee_name> 
            <street>124th</street> 
            <city>Omaha</city> 
            <gender>M</gender>
        </employee>
        <employee>
            <employee_name>Clemson, Ann M</employee_name> 
            <street>O</street> 
            <city>Lincoln</city> 
            <gender>F</gender>
        </employee>
        <employee>
            <employee_name>Dale, Mary K</employee_name> 
            <street>132th</street> 
            <city>Omaha</city> 
            <gender>F</gender>
        </employee>
        <employee>
            <employee_name>Gill, Mary L</employee_name> 
            <street>P Street</street> 
            <city>Lincoln</city> 
            <gender>F</gender>
        </employee>
        <employee>
            <employee_name>Harrison, Susan M</employee_name> 
            <street>Old Mill</street> 
            <city>Omaha</city> 
            <gender>F</gender>
        </employee>
        <employee>
            <employee_name>Jackson, Kim A</employee_name> 
            <street>178th</street> 
            <city>Omaha</city> 
            <gender>F</gender>
        </employee>
        <employee>
            <employee_name>Jason, Pat M</employee_name> 
            <street>8th</street> 
            <city>C.Bluffs</city> 
            <gender>M</gender>
        </employee>
        <employee>
            <employee_name>Kumar, Paul T</employee_name> 
            <street>Dodge</street> 
            <city>Omaha</city> 
            <gender>M</gender>
        </employee>
    </contents>
    <contents>
        <works>
            <employee_name>Anderson, Susan L</employee_name>
            <company_name>Mutual of Omaha</company_name>
            <salary>48000</salary>
        </works>
        <works>
            <employee_name>Brady, Dan L</employee_name>
            <company_name>FDR</company_name>
            <salary>42000</salary>
        </works>
        <works>
            <employee_name>Chen, Peter K</employee_name>
            <company_name>FDR</company_name>
            <salary>53000</salary>
        </works>
        <works>
            <employee_name>Clemson, Ann K</employee_name>
            <company_name>First Bank</company_name>
            <salary>39000</salary>
        </works>
        <works>
            <employee_name>Dale, Mary K</employee_name>
            <company_name>Mutual of Omaha</company_name>
            <salary>58000</salary>
        </works>
        <works>
            <employee_name>Gill, Mary L</employee_name>
            <company_name>Lincoln</company_name>
            <salary>48700</salary>
        </works>
        <works>
            <employee_name>Harrison, Susan M</employee_name>
            <company_name>Union Pacific</company_name>
            <salary>54320</salary>
        </works>
        <works>
            <employee_name>Jackson, Kim A</employee_name>
            <company_name>FDR</company_name>
            <salary>68000</salary>
        </works>
        <works>
            <employee_name>Jason, Pat M</employee_name>
            <company_name>FDR</company_name>
            <salary>83000</salary>
        </works>
        <works>
            <employee_name>Kumar, Paul T</employee_name>
            <company_name>FDR</company_name>
            <salary>44000</salary>
        </works>
    </contents>
    <contents>
        <company>
            <company_name>First Bank</company_name>
            <city>Omaha</city>
        </company>
        <company>
            <company_name>FDR</company_name>
            <city>Omaha</city>
        </company>
        <company>
            <company_name>Lincoln Star</company_name>
            <city>Lincoln</city>
        </company>
        <company>
            <company_name>Mutual of Omaha</company_name>
            <city>Omaha</city>
        </company>
        <company>
            <company_name>Union Pacific (UP)</company_name>
            <city>Omaha</city>
        </company>
    </contents>
</empdata>

有人可以帮帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您展示的数据中只有一名员工住在林肯并在奥马哈工作,即"Brady, Dan L""Clemson, Ann M"根本没有works条记录,"Gill, Mary L"适用于名为"Lincoln"且没有company条记录的公司。

您的查询存在的问题是,您不会检查works中的$w元素是否属于employee $e,您必须添加条件{{ 1}}。否则,你会得到每个林肯居民的结果,与奥马哈雇主的每个职位配对。

以下是您的查询的另一种表述,可能会使这三个部分之间的联系更加明显:

and $e/employee_name = $w/employee_name