博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL/T-SQL实例参考-2
阅读量:5214 次
发布时间:2019-06-14

本文共 2269 字,大约阅读时间需要 7 分钟。

 

对多关联查询,查询多中的记录,但是返回一的结果集

子查询语法

--一对多关联查询,查询多中的记录,但是返回一的结果集SELECT C.*  FROM (SELECT A.BasicIDFROM [tbiz_PuzzleBasic] A LEFT JOIN [tbiz_PuzzleWork] B ON B.BasicID = A.BasicIDWHERE B.Position LIKE '%经理%'GROUP BY A.BasicID ) T LEFT JOIN [tbiz_PuzzleBasic] C ON T.BasicID = C.BasicID

 

SqlBulkCopy

//string str = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();//SqlConnection conn = new SqlConnection(str);DataTable dtTdAnswerStorage = new DataTable();dtTdAnswerStorage.Columns.Add("QUESTIONID", typeof(int));dtTdAnswerStorage.Columns.Add("QUESTIONNAME", typeof(string));dtTdAnswerStorage.Columns.Add("SORTNUM", typeof(int));strQid = dt.Rows[0]["Qid"].ToString();foreach (OptionItem option in questionItem.OptionItems){    DataRow dr = dtTdAnswerStorage.NewRow();    dr["QUESTIONID"] = strQid;    dr["QUESTIONNAME"] = option.OptionTitle;    dr["SORTNUM"] = option.SortNum;    dtTdAnswerStorage.Rows.Add(dr);}using (SqlBulkCopy bulkCopy = new SqlBulkCopy(con)){    bulkCopy.BatchSize = 5000;    bulkCopy.BulkCopyTimeout = 60;    bulkCopy.DestinationTableName = "TD_ANSWER_STORAGE";    bulkCopy.ColumnMappings.Add("QUESTIONID", "QUESTIONID");    bulkCopy.ColumnMappings.Add("QUESTIONNAME", "QUESTIONNAME");    bulkCopy.ColumnMappings.Add("SORTNUM", "SORTNUM");    bulkCopy.WriteToServer(dtTdAnswerStorage);}

INSERT FROM SELECT

Insert Into TD_ANSWER(ANSWERID,QUESTIONID,SCALEID,QUESTIONNAME,SORTNUM) Select ANSWERID,QUESTIONID,{
0},QUESTIONNAME,SORTNUM From TD_ANSWER_STORAGE WHERE QUESTIONID ={
1};

UPDATE FROM SELECT

例子1:子查询嵌套+自增序列

UPDATE TD_QUESTION SET SORTNUM=M.IFROM(SELECT QUESTIONID,ROW_NUMBER() OVER(ORDER BY QUESTIONID ASC) As I FROM TD_QUESTION BWHERE SCALEID=21) MWHERE SCALEID=21

//所谓的链接子查询写法

UPDATE TD_QUESTION SET SORTNUM=M.IFROM TD_QUESTION A INNER JOIN(SELECT QUESTIONID,ROW_NUMBER() OVER(ORDER BY QUESTIONID ASC) As I FROM TD_QUESTION BWHERE SCALEID=21) M ON A.QUESTIONID= M.QUESTIONIDWHERE SCALEID=21

 处理NULL相关函数

isnull(check_expression , replacement_value ) ISNULL( T.recommendCount, 0 ) AS recommendCount,
CASE WHEN TBIZ_Contract.ContractID IS NULL

Group By

SELECT RoleID,ISNULL(Count(*),0) NUM FROM tbiz_UserInfo WHERE CompanyID=177 GROUP BY  RoleID HAVING RoleID=6

 

转载于:https://www.cnblogs.com/zhuji/p/8482024.html

你可能感兴趣的文章
滚动条
查看>>
程序员的自我修养九Windows下的动态链接
查看>>
Codeforces Round #361 (Div. 2)
查看>>
Python time & datetime & string 相互转换
查看>>
细说WebSocket - Node篇
查看>>
java.lang.UnsupportedOperationException
查看>>
Linux operating system (Ubuntu) 学习-1
查看>>
Python字典实现分析
查看>>
jenkins+testNG
查看>>
Java自定义范型的应用技巧
查看>>
[洛谷1485] 火枪打怪
查看>>
白话经典算法系列之六 快速排序 快速搞定
查看>>
错了:用流量能够放肆,有wifi则要节制
查看>>
https://zhidao.baidu.com/question/362784520674844572.html
查看>>
【MFC 学习笔记】CFile读写文件
查看>>
PAT B1018.锤子剪刀布(20)
查看>>
Extjs控件之 grid打印功能
查看>>
枚举类型(不常用)递归
查看>>
ETL
查看>>
Tomcat源码分析(六)--日志记录器和国际化
查看>>