一、Left Join 两种情况
1、A表有5个,以A表为条件,查询满足跟着A行,不满足的字段为Null, 共计5行;
2、A表有5个,以A表为条件,查询满足A表一条记录的有两条,那个显示共计6行;
二、子查询 两种情况
1、两种情况,第一个 查询字段都来自一个表,个别字段(三四个字段来别别的表的情况下--left join 适合多字段)
2、从结果集中查询的时候使用--(分两种,1、当筛选条件来查询用子查询的结果时,筛选的条件是子查询里sql的列名,要想解决:
1、提前判断是否包含该子sql列名去查询 取值 ,找到关联的外层sql字段 限制。
2、还是加字查询,将查询的结果集(包含子列)的结果当做结果集----因为结果集包含子的列,用select * from 该结果集 ) ,当然这里,where 的条件就要加在 外面,拼接的sql是整体到数据库查询,
例如 :
代码如下:
public int GetDataTableByCount2(string Where) { string Sql = "select Id,CityId,CreateUser,AreaName,MoneySelf,LoanNo,adddate CreateTime,ProductName,BitType,PersonName,MoneyOther,ExpectedMoney,MoneyCredit,MoneyReal,AgentUser,HouseUser,RiskCtrlUser,CheckUser,HouseArea,Status,(select ClassName from Tbl_Class where Id = a.CityId) as city,(select AdminNickName from tbl_admin where AdminName = a.AgentUser) as QuDaoNickName, (select AdminNickName from tbl_admin where AdminName = a.HouseUser) as HouseUserNickName,(select AdminNickName from tbl_admin where AdminName = a.RiskCtrlUser) as RiskCtrlUserNickName,(select AdminNickName from tbl_admin where AdminName = a.CreateUser) as JinJianNickName,(select CreateUser from tbl_admin where AdminName = a.AgentUser) as YeWuZhuanYuan,('冯东君') as YeWuZhuGuan,('江威') as RiskZhuGuan,('洪文') as CheckZhuGuan,(select top 1 AdminNickName from tbl_admin where AdminName = (select CreateUser from tbl_admin where AdminName = a.AgentUser) )as YeWuZhuanYuanNickName,(select top 1 TotalAmount from tbl_pay where LoanNo = a.LoanNo and TradeSubject='服务费') as HouseMoney ,(select top 1 300 from tbl_pay where LoanNo = a.LoanNo and IsOverTime = 1) as HouseMoneyElse from tbl_loan a where dealflag=0"; if (Where != "") Sql = "select count(*) from (" + Sql + ") tbl where " + Where; //结果集 满足 where 查询条件的 总数 (里面 结果集中包含子sql 的列) int RecordNum = (int)DBHelper.ExecuteScalar(this.connection, CommandType.Text, Sql); return RecordNum; } // 返回条件返回分页数据 /// </summary> public DataTable GetDataTableByPage2(int PageSize, int PageIndex, string Where, string Order) { //string Sql = "select *,(select count(*) from tbl_loanpic where dealflag=0 and lp_classbig='视频' and loanid=Tbl_Loan.id) as loanvideonum,(select count(*) from tbl_loanpic where dealflag=0 and lp_classbig='下户' and loanid=Tbl_Loan.id) as loanpicnum,(select isnull(min(adddate),'') from tbl_loanpic where dealflag=0 and lp_classbig='下户' and loanid=Tbl_Loan.id) as loanpictime," + GetDataTableByCount2(Where) + " as RecordNum,(select top 1 adminnickname from Tbl_Admin where adminname=Tbl_Loan.AgentUser) as AgentName,(select top 1 adminlevel from Tbl_Admin where adminname=Tbl_Loan.AgentUser) as AgentLevel,(select top 1 adminnickname from Tbl_Admin where adminname=Tbl_Loan.CreateUser) as CreateName,(select count(1) from Tbl_Loan a where a.PersonName= Tbl_Loan.PersonName and a.DealFlag=0 and a.CityId=Tbl_Loan.CityId having count(1) >1) count from Tbl_Loan where [DealFlag]=0"; string Sql = "select Id,CityId,CreateUser,AreaName,MoneySelf,LoanNo,adddate CreateTime,ProductName,BitType,PersonName,MoneyOther,ExpectedMoney,MoneyCredit,MoneyReal,AgentUser,HouseUser,RiskCtrlUser,CheckUser,HouseArea,Status,(select ClassName from Tbl_Class where Id = a.CityId) as city,(select AdminNickName from tbl_admin where AdminName = a.AgentUser) as QuDaoNickName, (select AdminNickName from tbl_admin where AdminName = a.HouseUser) as HouseUserNickName,(select AdminNickName from tbl_admin where AdminName = a.CreateUser) as JinJianNickName,(select CreateUser from tbl_admin where AdminName = a.AgentUser) as YeWuZhuanYuan,('冯东君') as YeWuZhuGuan,('江威') as RiskZhuGuan,('洪文') as CheckZhuGuan,(select top 1 AdminNickName from tbl_admin where AdminName = (select CreateUser from tbl_admin where AdminName = a.AgentUser) )as YeWuZhuanYuanNickName,(select top 1 TotalAmount from tbl_pay where LoanNo = a.LoanNo and TradeSubject='服务费') as HouseMoney ,(select top 1 300 from tbl_pay where LoanNo = a.LoanNo and IsOverTime = 1) as HouseMoneyElse from tbl_loan a where dealflag=0"; if (Where != "") Sql = "select * from ("+ Sql + ") tbl where " + Where;//在结果集中 查询 满足 where 条件的数据 if (Order != "") Sql += " order by " + Order; int startRecord = PageSize * (PageIndex - 1); int endRecord = PageSize; DataTable dt = DBHelper.ExecuteDataTablePage(this.connection, CommandType.Text, Sql, startRecord, endRecord); return dt; } public dynamic GetDataTableByCount3(int PageSize, int PageIndex, string Where, string Order)//在结果集中统计满足条件的金额 { string Sql = "select Id,CityId,CreateUser,AreaName,MoneySelf,LoanNo,adddate CreateTime,ProductName,BitType,PersonName,MoneyOther,ExpectedMoney,MoneyCredit,MoneyReal,AgentUser,HouseUser,RiskCtrlUser,CheckUser,HouseArea,Status,(select ClassName from Tbl_Class where Id = a.CityId) as city,(select AdminNickName from tbl_admin where AdminName = a.AgentUser) as QuDaoNickName,(select AdminNickName from tbl_admin where AdminName = a.HouseUser) as HouseUserNickName, (select AdminNickName from tbl_admin where AdminName = a.RiskCtrlUser) as RiskCtrlUserNickName,(select AdminNickName from tbl_admin where AdminName = a.CreateUser) as JinJianNickName,(select CreateUser from tbl_admin where AdminName = a.AgentUser) as YeWuZhuanYuan,('冯东君') as YeWuZhuGuan,('江威') as RiskZhuGuan,('洪文') as CheckZhuGuan,(select top 1 AdminNickName from tbl_admin where AdminName = (select CreateUser from tbl_admin where AdminName = a.AgentUser) )as YeWuZhuanYuanNickName,(select top 1 TotalAmount from tbl_pay where LoanNo = a.LoanNo and TradeSubject='服务费') as HouseMoney ,(select top 1 300 from tbl_pay where LoanNo = a.LoanNo and IsOverTime = 1) as HouseMoneyElse from tbl_loan a where dealflag=0"; //if (Where != "") Sql += " and " + Where; //if (Order != "") Sql += " order by " + Order; string Sql1 = "select sum(MoneyReal) SumMoneyreal from (" + Sql + ") temp where "+ Where;//总放款金额 int result1 = 0; var res = DBHelper.ExecuteScalar(this.connection, CommandType.Text, Sql1); if (!Convert.IsDBNull(res)) { result1 = Convert.ToInt32(res); } string Sql2 = "select sum(HouseMoney) SumHouseMoney from (" + Sql + ") temp where " + Where;//总下户费 object res2 = DBHelper.ExecuteScalar(this.connection, CommandType.Text, Sql2); decimal result2=0; if (!Convert.IsDBNull(res2)) { result2 = Convert.ToDecimal(res2); } Tuple<int, decimal> result = new Tuple<int, decimal>(result1, result2); return result; }
三、数据多行转一行
比如有一个表,里面有三行数据,name分别是: name 01 02 03 现在要把这三个值转换成一行,显示为: name 01,02,03
就会用到这个,for xml path 进行拼接
三、排序,也可以去掉重复(过滤有多条的数据,而非真正意义去重)
--单独 order by 对 loanId 排序 select id,ROW_NUMBER() over(order by loanId) as rows from Tbl_LoanLog ----partition by 必须使用 loanId 排序 select id,ROW_NUMBER() over(partition by loanId order by loanId) as rows from Tbl_LoanLog ----partition by 必须使用 loanId 排序 但是为了 row 是一个 SELECT * FROM ( SELECT a.status,a.id,ROW_NUMBER() over(partition by loanId order by loanId) as rows from Tbl_LoanLog LEFT JOIN Tbl_Loan a ON Tbl_LoanLog.LoanID=a.Id ) AS vmtab where vmtab.rows =1 ----但是但是还是没有解决 ,我需要的去重的数据 ,而非去掉有多行的数据 用distinct
DISTINCT的使用 distinct必须放在开头
(实际上是根据LoanID和status、Id、CityId,CreateUser、AreaName 六个字段来去重的)
简化
最终解决
首先:
理清关系
-- 首先 Tbl_Loan 表 a表 Tbl_LoanLog b表 -- 情况 a表有正式和测试数据两种, a正式数据则 b表有对应记录(多条,随着a表的状态,就会生成一条),a表里测试数据,则b 表是没有任何生成记录的。 -- 显示a表的正式数据 --注意: a表里面状态会变, 比如 a 表 tbll.Status=N'评估完成' 则有 1252 ,状态具体有(评估完成/ 待审核,已审核...) --而通过 b 表记录 评估完成的 的进件 1818 则是因为总件数的原因 , 现在停留在评估完成的状态 有1252 , --原本1818-1252 剩下的则是进入其他状态的,log状态日志记录是唯一的,所以现在直接查询a表是是很少的,那是因为当前的进件状态 -- 这种方式 1819条 以数据表为准的 select count(*) from Tbl_Loan a where a.Id in (SELECT DISTINCT b.LoanId from Tbl_LoanLog b where b.status='评估完成') --以下都是 log 表为准的 条数 --子查询方式 1818 条 SELECT count(*) FROM ( select tbll.status,tbl.ID from Tbl_LoanLog as tbll LEFT JOIN Tbl_Loan as tbl ON tbll.LoanID=tbl.Id WHERE tbl.DealFlag=0 and tbll.DealFlag=0 ) AS vmtab where status='评估完成' --left 方式 1818 条 select Count(*) from Tbl_LoanLog as tbll LEFT JOIN Tbl_Loan as tbl ON tbll.LoanID=tbl.Id WHERE tbl.DealFlag=0 and tbll.DealFlag=0 and tbll.status='评估完成' --这种方式 也是 1818 条 select SUM(CASE when tbll.status='评估完成' THEN 1 ELSE 0 END ) as TotalNum from Tbl_LoanLog as tbll LEFT JOIN Tbl_Loan as tbl ON tbll.LoanID=tbl.Id WHERE tbl.DealFlag=0 and tbll.DealFlag=0 --排除log 评估状态根据 LoanId分组后状态 评估完成是否会出现两条 select a.LoanId from Tbl_LoanLog a where status='评估完成' GROUP BY a.LoanId HAVING count(a.loanId) = 2
最终还是没有解决重复
解决办法 select * from Tbl_Loan where in id in ( select LoanId from Tbl_LoanLog) --in 不用考虑 log 里面的