Database component Hxj.data (13) (subquery)

Source: Internet
Author: User

The value of a query condition comes from another query.

Like SQL Server sql:

SELECT * from the products where categoryid= (select top 1 CategoryID from categories where categoryname= ' produce ')

The implementation code for this SQL is as follows:

DbSession.Default.From<Products>()
         .Where(Products._.CategoryID
         .SubQueryEqual(DbSession.Default.From<Categories>().Where(Categories._.CategoryName ==  "Produce").Select(Categories._.CategoryID).Top(1)))
         .ToList();

Compare the SQL generated by the component

Text:
SELECT * FROM [Products]
WHERE [Products].[CategoryID] = ( SELECT TOP 1 [Categories].[CategoryID] FROM [Categories] WHERE [Categories]. [CategoryName] = @174b5c8999e2480594cdc08ab4d8e5bd)
Parameters:
@174b5c8999e2480594cdc08ab4d8e5bd[String] = Produce

The subquery method corresponds to the SQL table as follows:

" /tr>
method name sql
=
<>
subqueryless <
subquerylessorequal &L t;=
subquerygreater >
>=
i N
not in

Write an example again

DbSession.Default.From<Products>()
         .Where(Products._.CategoryID
         .SubQueryNotIn(DbSession.Default.From<Categories>().Where(Categories._.CategoryName ==  "Produce").Select(Categories._.CategoryID)))
         .ToList();

The generated SQL is as follows

Text:
SELECT * FROM [Products]
WHERE [Products].[CategoryID]
NOT IN ( SELECT [Categories].[CategoryID] FROM [Categories] WHERE [Categories].[CategoryName] =  @32365a219b864e5fbeb7959a6071d4c8)
Parameters:
@32365a219b864e5fbeb7959a6071d4c8[String] = Produce

The subquery is also changed very simple.

The following section describes the federated query.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.