如何使用Nhibernate查询包

时间:2010-12-20 17:17:21

标签: c# nhibernate many-to-many subquery

我需要获取国家/地区列表中不包含国家/地区的出版物(研究IsoCode2)

sql查询是:

select * from pub_head ph
where not exists
(select 1 from pub_head_forbidden_country phfc , country c
                where phfc.pub_head_id = ph.pub_head_id 
                and phfc.country_id = c.country_id 
                and c.iso_code2 = 'CA');

模特:

<class name="Publication" table="PUB_HEAD">

  <id name="Id" column="PUB_HEAD_ID">
   <generator class="native">
    <param name="sequence">SEQ_PUB_HEAD</param>
   </generator>
  </id>

  <idbag name="Countries" table="PUB_HEAD_COUNTRY" lazy="true">
   <collection-id column="PUB_HEAD_COUNTRY_ID">
    <generator class="native">
     <param name="sequence">SEQ_PUB_HEAD_COUNTRY</param>
    </generator>
   </collection-id>

   <key column ="PUB_HEAD_ID"  />
   <many-to-many class="Model.Referential.Country, Model" column="COUNTRY_ID"/>
  </idbag>
</class>

<class name="Country" table="Country">
  <id name="Id" column="COUNTRY_ID">
   <generator class="native">
   </generator>
  </id>
  <property name="Name">
   <column name="NAME"></column>
  </property>
  <property name="IsoCode2">
   <column name="ISO_CODE2"></column>
  </property>
  <property name="IsoCode3">
   <column name="ISO_CODE3"></column>
  </property>

 </class>

我从SubQueries开始,但我没有成功。

由于

2 个答案:

答案 0 :(得分:0)

对加入过敏? SQL非常难理解。

这是不是意味着这个?

SELECT *
FROM pub_head
WHERE id not IN (
  SELECT phfc.pub_head_id
  FROM pub_head_forbidden_country as phfc
    INNER JOIN country AS c ON phfc.country_id = c.country_id
  WHERE c.iso_code2 = 'CA'
)

[发布回答是因为这条SQL在评论中会很糟糕。]

答案 1 :(得分:0)

Sql也可能是这样的

SELECT * FROM pub_head ph
WHERE ph.pub_head_id not IN (
  SELECT phfc.pub_head_id
  FROM pub_head_forbidden_country phfc
    INNER JOIN country  c ON phfc.country_id = c.country_id
  WHERE c.iso_code2 = 'CA'
)