自研ORM嵌套查询和子查询,强不强大您说了算。
测试代码
var count = 0;
var refAsync = new RefAsync<int>();
//下面示例方法的重载均支持
var query = db.Query<Product>().Select(s => new
{
WithAttr_First = db.QueryWithAttr<Product>().First(),
WithAttr_FirstAsync = db.QueryWithAttr<Product>().FirstAsync(),
WithAttr_ToList = db.QueryWithAttr<Product>().ToList(),
WithAttr_ToListAsync = db.QueryWithAttr<Product>().ToListAsync(),
First_1 = db.Query<Category>().Select(s => 1).First(),//解析成Sql
First = db.Query<Category>().First(),
FirstAsync = db.Query<Category>().FirstAsync(),
ToArray = db.Query<Category>().ToArray(),
ToArrayAsync = db.Query<Category>().ToArrayAsync(),
ToList = db.Query<Category>().ToList(),
ToListAsync = db.Query<Category>().ToListAsync(),
ToPageList = db.Query<Category>().ToPageList(1, 10),
ToPageListAsync = db.Query<Category>().ToPageListAsync(1, 10),
ToPageList_Count = db.Query<Category>().ToPageList(1, 10, ref count),
ToPageListAsync_Count = db.Query<Category>().ToPageListAsync(1, 10, refAsync),
ToDictionary = db.Query<Category>().ToDictionary(),
ToDictionaryAsync = db.Query<Category>().ToDictionaryAsync(),
ToDictionaryList = db.Query<Category>().ToDictionaryList(),
ToDictionaryListAsync = db.Query<Category>().ToDictionaryListAsync(),
ToDictionaryPageList = db.Query<Category>().ToDictionaryPageList(1, 10),
ToDictionaryPageListAsync = db.Query<Category>().ToDictionaryPageListAsync(1, 10),
ToDictionaryPageList_Count = db.Query<Category>().ToDictionaryPageList(1, 10, ref count),
ToDictionaryPageListAsync_Count = db.Query<Category>().ToDictionaryPageListAsync(1, 10, refAsync),
ToDataTable = db.Query<Category>().ToDataTable(),
ToDataTableAsync = db.Query<Category>().ToDataTableAsync(),
ObjToJson = db.Query<Category>().ObjToJson(),
ObjToJsonAsync = db.Query<Category>().ObjToJsonAsync(),
ObjListToJson = db.Query<Category>().ObjListToJson(),
ObjListToJsonAsync = db.Query<Category>().ObjListToJsonAsync(),
Max = db.Query<Category>().Max(a => a.CategoryId),//解析成Sql
MaxAsync = db.Query<Category>().MaxAsync(a => a.CategoryId),
Min = db.Query<Category>().Min(a => a.CategoryId),//解析成Sql
MinAsync = db.Query<Category>().MinAsync(a => a.CategoryId),
Count = db.Query<Category>().Count(),//解析成Sql
CountAsync = db.Query<Category>().CountAsync(),
Sum = db.Query<Product>().Sum(s => s.Number),//解析成Sql
SumAsync = db.Query<Product>().SumAsync(s => s.Number),
Avg = db.Query<Product>().Avg(s => s.Number),//解析成Sql
AvgAsync = db.Query<Product>().AvgAsync(s => s.Number)
});
var data = query.First();
//支持所有重载方法,子查询 一定要调用 返回结果的方法 和普通查询一样
var json = db.Query<Product>().Select(a => new
{
Max_Query = db.Query<Category>().Where(x => x.CategoryId == a.CategoryId).Max(a => a.CategoryId),
Min_Query = db.Query<Category>().OrderBy(o => o.CategoryId, OrderByType.DESC).Min(a => a.CategoryName),
Count_Query = db.Query<Category>().OrderBy(o => o.CategoryId, OrderByType.DESC).Count(),
Avg_Query = db.Query<Product>().Avg(a => a.Number),
Sum_Query = db.Query<Product>().Sum(a => a.Number),
Sum_Query_2 = db.Query<Product>().Sum<decimal>("Number"),
TestQuery = db.Query<Category>()
.InnerJoin<Product>((a, b) => a.CategoryId == b.CategoryId)
.Select((a, b) => new
{
a.CategoryId,
a.CategoryName,
b.ProductCode,
b.ProductName,
XX = db.Query<Category>()
// 特别注意:同一个Where 只能引用一个上级别名 如果当前别名和上级别名一致则优先解析当前别名
.Where(c => c.CategoryId == a.CategoryId)
.Where(c => c.CategoryId == b.CategoryId).First()
}).First(),
//甚至可以在这里使用ado方式查询,虽然不推荐,但还是很厉害的
Ado_Test = db.Ado.ExecuteReader(System.Data.CommandType.Text, "select 1", null).ListBuild<int>(),
}).ObjToJson();
Console.WriteLine(json);
解析后Sql
SELECT p2.`ProductId`,p2.`CategoryId`,p2.`ProductCode`,p2.`ProductName`,p2.`DeleteMark`,p2.`CreateTime`,p2.`ModifyTime`,p2.`Custom1`,p2.`Custom2`,p2.`Custom3`,p2.`Custom4`,p2.`Custom5`,p2.`Custom6`,p2.`Custom7`,p2.`Custom8`,p2.`Custom9`,p2.`Custom10`,p2.`Custom11`,p2.`Custom12`,p2.`Number` FROM `Product` `p2` Limit 1
SELECT p2.`ProductId`,p2.`CategoryId`,p2.`ProductCode`,p2.`ProductName`,p2.`DeleteMark`,p2.`CreateTime`,p2.`ModifyTime`,p2.`Custom1`,p2.`Custom2`,p2.`Custom3`,p2.`Custom4`,p2.`Custom5`,p2.`Custom6`,p2.`Custom7`,p2.`Custom8`,p2.`Custom9`,p2.`Custom10`,p2.`Custom11`,p2.`Custom12`,p2.`Number` FROM `Product` `p2` Limit 1
SELECT p2.`ProductId`,p2.`CategoryId`,p2.`ProductCode`,p2.`ProductName`,p2.`DeleteMark`,p2.`CreateTime`,p2.`ModifyTime`,p2.`Custom1`,p2.`Custom2`,p2.`Custom3`,p2.`Custom4`,p2.`Custom5`,p2.`Custom6`,p2.`Custom7`,p2.`Custom8`,p2.`Custom9`,p2.`Custom10`,p2.`Custom11`,p2.`Custom12`,p2.`Number` FROM `Product` `p2`
SELECT p2.`ProductId`,p2.`CategoryId`,p2.`ProductCode`,p2.`ProductName`,p2.`DeleteMark`,p2.`CreateTime`,p2.`ModifyTime`,p2.`Custom1`,p2.`Custom2`,p2.`Custom3`,p2.`Custom4`,p2.`Custom5`,p2.`Custom6`,p2.`Custom7`,p2.`Custom8`,p2.`Custom9`,p2.`Custom10`,p2.`Custom11`,p2.`Custom12`,p2.`Number` FROM `Product` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` Limit 1
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` Limit 1
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` LIMIT 0,10
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` LIMIT 0,10
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` LIMIT 0,10
SELECT COUNT( 1 ) FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` LIMIT 0,10
SELECT COUNT( 1 ) FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` LIMIT 0,10
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` LIMIT 0,10
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` LIMIT 0,10
SELECT COUNT( 1 ) FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` LIMIT 0,10
SELECT COUNT( 1 ) FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` Limit 1
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2` Limit 1
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT p2.`CategoryId`,p2.`CategoryName` FROM `Category` `p2`
SELECT MAX( `p2`.`CategoryId` ) AS `CategoryId` FROM `Category` `p2`
SELECT MIN( `p2`.`CategoryId` ) AS `CategoryId` FROM `Category` `p2`
SELECT COUNT( 1 ) FROM `Category` `p2`
SELECT SUM( `p2`.`Number` ) AS `Number` FROM `Product` `p2`
SELECT AVG( `p2`.`Number` ) AS `Number` FROM `Product` `p2`
SELECT 0 AS `fast_args_index_0`,1 AS `fast_args_index_1`,2 AS `fast_args_index_2`,3 AS `fast_args_index_3`,( SELECT 1 FROM `Category` `p2` Limit 1 ) AS `First_1`,5 AS `fast_args_index_5`,6 AS `fast_args_index_6`,7 AS `fast_args_index_7`,8 AS `fast_args_index_8`,9 AS `fast_args_index_9`,10 AS `fast_args_index_10`,11 AS `fast_args_index_11`,12 AS `fast_args_index_12`,13 AS `fast_args_index_13`,14 AS `fast_args_index_14`,15 AS `fast_args_index_15`,16 AS `fast_args_index_16`,17 AS `fast_args_index_17`,18 AS `fast_args_index_18`,19 AS `fast_args_index_19`,20 AS `fast_args_index_20`,21 AS `fast_args_index_21`,22 AS `fast_args_index_22`,23 AS `fast_args_index_23`,24 AS `fast_args_index_24`,25 AS `fast_args_index_25`,26 AS `fast_args_index_26`,27 AS `fast_args_index_27`,28 AS `fast_args_index_28`,( SELECT MAX( `p2`.`CategoryId` ) AS `CategoryId` FROM `Category` `p2` ) AS `Max`,30 AS `fast_args_index_30`,( SELECT MIN( `p2`.`CategoryId` ) AS `CategoryId` FROM `Category` `p2` ) AS `Min`,32 AS `fast_args_index_32`,( SELECT COUNT( 1 ) FROM `Category` `p2` ) AS `Count`,34 AS `fast_args_index_34`,( SELECT SUM( `p2`.`Number` ) AS `Number` FROM `Product` `p2` ) AS `Sum`,36 AS `fast_args_index_36`,( SELECT AVG( `p2`.`Number` ) AS `Number` FROM `Product` `p2` ) AS `Avg`,38 AS `fast_args_index_38` FROM `Product` Limit 1
SELECT p4.`CategoryId`,p4.`CategoryName`,p3.`ProductId`,p3.`ProductCode`,p3.`ProductName`,p3.`DeleteMark`,p3.`CreateTime`,p3.`ModifyTime`,p3.`Custom1`,p3.`Custom2`,p3.`Custom3`,p3.`Custom4`,p3.`Custom5`,p3.`Custom6`,p3.`Custom7`,p3.`Custom8`,p3.`Custom9`,p3.`Custom10`,p3.`Custom11`,p3.`Custom12`,p3.`Number` FROM `Category` `p4`
RIGHT JOIN `Category` `p2` ON ( `p4`.`CategoryId` = `p2`.`CategoryId` )
RIGHT JOIN `Product` `p3` ON ( `p4`.`CategoryId` = `p3`.`CategoryId` ) Limit 1
SELECT `p2`.`CategoryId` AS `CategoryId`,`p2`.`CategoryName` AS `CategoryName`,`p3`.`ProductCode` AS `ProductCode`,`p3`.`ProductName` AS `ProductName`,4 AS `fast_args_index_4` FROM `Category` `p2`
INNER JOIN `Product` `p3` ON ( `p2`.`CategoryId` = `p3`.`CategoryId` ) Limit 1
select 1
SELECT ( SELECT MAX( `p2`.`CategoryId` ) AS `CategoryId` FROM `Category` `p2` ) AS `Max_Query`,( SELECT MIN( `p2`.`CategoryName` ) AS `CategoryName` FROM `Category` `p2`
ORDER BY `p2`.`CategoryId` DESC ) AS `Min_Query`,( SELECT COUNT( 1 ) FROM `Category` `p2`
ORDER BY `p2`.`CategoryId` DESC ) AS `Count_Query`,( SELECT AVG( `p2`.`Number` ) AS `Number` FROM `Product` `p2` ) AS `Avg_Query`,( SELECT SUM( `p2`.`Number` ) AS `Number` FROM `Product` `p2` ) AS `Sum_Query`,( SELECT SUM( Number ) FROM `Product` `p2` ) AS `Sum_Query_2`,6 AS `fast_args_index_6`,7 AS `fast_args_index_7` FROM `Product` Limit 1
{"Max_Query":4,"Min_Query":"测 试 ","Count_Query":4,"Avg_Query":0,"Sum_Query":0,"Sum_Query_2":0,"TestQuery":{"CategoryId":1,"CategoryName":"测 试 ","ProductCode":"测 试 编 号 _1686464371_6","ProductName":"测 试 名 称 _1686464371_6","XX":{"CategoryId":1,"CategoryName":"测 试 "}},"Ado_Test":[1]}
From子查询
var subQuery = db.Query<Product>();
var data = db.Query(subQuery).OrderBy(o => o.ProductCode).ToList();
Join子查询
var subQuery = db.Query<Product>();
var data = db.Query<Product>().InnerJoin(subQuery, (a, b) => a.ProductId == b.ProductId).ToList();
自研ORM嵌套查询和子查询,强不强大您说了算。的更多相关文章
- SQL——嵌套查询与子查询
前言 sql的嵌套查询可以说是sql语句中比较复杂的一部分,但是掌握好了的话就可以提高查询效率.下面将介绍带in的子查询.带比较运算符的子查询.带any/all的子查询.带exists的子查询以及基于 ...
- MySQL 嵌套子查询 with子句 from子查询 in子查询 join组合
一.适用场景和方法 (1)适用场景 考虑查询过程中是否存在以下情况: 查询某些数据时需要分组才能得到,某些数据不需要分组就能得到或者分组条件不同: 查询某些数据时需要where条件,某些列不需要whe ...
- Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)
Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...
- SQL编程之高级查询(子查询)以及注意事项
SQL编程之高级查询(子查询)以及注意事项 1.什么是子查询? 当一个查询是另一个查询的条件时,称之为子查询.子查询可以使用几个简单命令构造功能强大的复合命令.子查询最常用于SELECT-SQL命 ...
- Sql Server的艺术(六) SQL 子查询,创建使用返回多行的子查询,子查询创建视图
子查询或内部查询或嵌套查询在另一个SQL查询的查询和嵌入式WHERE子句中. 子查询用于返回将被用于在主查询作为条件的数据,以进一步限制要检索的数据. 子查询可以在SELECT,INSERT,UPDA ...
- mysql之连接查询、联合查询、子查询
本文内容: 连接查询 联合查询 子查询 from子查询 where子查询 exists子查询 首发日期:2018-04-11 连接查询: 连接查询就是将多个表联合起来查询,连接查询方式有内连接.外连接 ...
- MySQL之多表查询一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习
MySQL之多表查询 阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都会建 ...
- Server Sql 多表查询、子查询和分页
一.多表查询:根据特定的连接条件从不同的表中获取所需的数据 多表查询语法: SELECT table1.column, table2.column FROM table1, table2 WHERE ...
- sqlserver查询(子查询,全连接,等值连接,自然连接,左右连,交集,并集,差集)
--部门表 create table dept( deptno int primary key,--部门编号 dname ),--部门名 loc )--地址 ); --雇员表 create table ...
- oracle多表关联查询和子查询
oracle多表关联查询和子查询 一.多表关联查询 例子: SQL> create table student1 ( sid ), sname ), sage )); Table created ...
随机推荐
- 单元测试Mockito框架
单元测试Mockito框架 Mock 测试就是在测试过程中,对于某些 不容易构造(如 HttpServletRequest 必须在 Servlet 容器中才能构造出来)或者不容易获取 比较复杂 的对象 ...
- computed的setter妙用
使用场景:当我们用v-model绑定了一个计算属性,想直接设置计算属性时,就要利用到setter demo: <template> <div> <div>First ...
- node.js解决跨域方案
服务端 1.通过使用cors模块解决跨域问题 var express = require('express') , cors = require('cors') , app = express(); ...
- 原生请求 js、jquery封装的ajax请求、axios请求与fetch请求区别与优缺点
原生JS请求 现代浏览器,最开始与服务器交换数据,都是通过XMLHttpRequest对象.它可以使用JSON.XML.HTML和text文本等格式发送和接收数据. 首先我们先把原生的请求封装一下: ...
- 智能且集成的端到端移动应用程序安全解决方案——Quixxi简介
移动应用程序安全变得简单快捷 Quixxi 是一种智能且集成的端到端移动应用程序安全解决方案.这个强大的工具可供开发人员在几分钟内保护和监控任何移动应用程序. Quixxi Security 评估应用 ...
- 《HelloTester》第4期
1.前言 终于到了谈面试的部分了! 我在这也说明一下,有同学说之前简历篇的时候一直在说项目的介绍,而面试官真正关心的是技术啊?我在这做个解释,因为我写的这些文章主要针对的是软件测试的同学,所以其他职位 ...
- 1 Android开发书籍
不管你是Android菜鸟还是Android高手,一定能够找到一本适合自己阅读的书籍.下面为大家推荐8本书. <Android进阶之光> <Android进阶之光>详细并深入讲 ...
- pnpm 之降本增效
作者:京东科技 于振京 受众简介 前端研发工程师 还在为npm i安装大量依赖等待时间较长,npm扁平化node_modules依赖版本冲突在苦恼吗,不用苦恼pnpm为你保驾护航 主要影响:安装依赖包 ...
- ServletContext 详解(转载)
转载:https://www.cnblogs.com/zjdxr-up/p/7761813.html ServletContext,是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释 ...
- day66:Linux:nginx+uwsgi+django跑python项目
目录 0.uwsgi简述 1.使用uwsgi+django运行demo 2.nginx+uwsgi+django跑pythonav项目 0.uwsgi简述 1.什么是wsgi WSGI,全称Web S ...