sql 子查询  相关…

本文深入解析了SQL中的子查询概念,区分了不相关子查询(嵌套子查询)与相关子查询的不同,并通过实例展示了如何使用子查询来解决复杂查询问题。

select id,fpbl from j_pbxx p where id_fpb = (select id_fpb from j_pbxx where id='0400000000002N') and sffp='是'
子查询 通常 在where 子句中
comparison [ANY | ALL | SOME] (sqlstatement)
expression [NOT] IN (sqlstatement)
[NOT] EXISTS (sqlstatement)

SELECT * FROM Products
WHERE UnitPrice > ANY
(SELECT UnitPrice FROM OrderDetails

WHERE Discount >= .25);


SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
HAVING
MIN(salary) >
(SELECT MIN(salary)
FROM employees
WHERE department_id = 50);
求每个部门的最小工资,但是要高于50号部门的工资。

上面是不相关子查询 非相关子查询(嵌套子查询):
不相关子查询是指子查询独立于外层语句(主查询),他不依赖于其外层语句的操作结果,他们执行时可分为两个独立的步骤,即先执行子查询,在执行外层查询
SELECT 姓名 FROM STUDENT WHERE入学成绩>(select avg(入学成绩) FROM STUDENT )
当子查询跟随在 =、!=、<、<=、>、>= 之后, 子查询的返回值只能是一个, 否则应在外层where子句中用
一个in限定符,即要返回多个值,要用in或者not in


相关子查询:

select * from work a where 基本工资=(select max(基本工资) from work b where a.部门名称=b.部门名称)
说明:由外查询提供一个部门名称给内查询,内查询利用这个部门名称找到该部门的最高基本工资,然后外查询根据基本工资判断是否等于最高工资,如果是的,则显示出来.
相当于:select * from work,(select 部门名称,max(基本工资) as 基本工资 from work group by 部门名称 as t)
where work.基本工资=t.基本工资 and work.部门名称=t.部门名称

区别相关子查询,非相关子查询.就是看子查询中用到没用到外层查询的东西. 子查询是否可独立执行

特征

1.非相关子查询是独立于外部查询的子查询,子查询总共执行一次,执行完毕后将值传递给外部查询。 
2.相关子查询的执行依赖于外部查询的数据,外部查询执行一行,子查询就执行一次。

相关子查询中,外部的每一行都要带入到子查询计算(外部有几行,子查询就执行几次)

非相关子查询只需执行一次.     

差别还是挺大的.效率也是. 相关子查询也许可以调优

<!-- ==================== 主表单结构 ==================== --> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableForm" style="table-layout: fixed;"> <colgroup> <col width="80" /> <col /><!--hide4phone.start--> <col width="80" /> <col width="380" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td style="text-align: right;">&nbsp;<span style="color: red;">*</span>Subject:</td> <td dbf.type="required" id="dbf.subject">&nbsp;</td> <!--show4phone.start--> </tr> <tr><!--show4phone.end--> <td style="text-align: right;">&nbsp;Status:</td> <td><span id="mapping.dbf.procXSource">&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Responsor: <span id="mapping.dbf.responsorSource">&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Participants: <span id="mapping.dbf.participantsSource">&nbsp;</span></td> </tr> </tbody> </table> <div>&nbsp;</div> <!-- ==================== 页面标题 ==================== --> <div style="text-align: center;"> <h1><img src="../common/logo.png" /> [报价历史查询内勤用]</h1> </div> <div>[Design form here, based on the template below, or customized by yourself after the template removed]</div> <!-- ==================== 查询输入区域 ==================== --> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder" style="table-layout: fixed; width: 928px;"> <colgroup> <col width="130" /> <col /><!--hide4phone.start--> <col width="130" /> <col width="330" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td colspan="4" style="background-color: lightyellow;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center; width: 147px;"><span style="color: red;">*</span>&nbsp;username</td> <td id="username" style="width: 210px;">&nbsp;</td> <!--show4phone.start--> </tr> <tr><!--show4phone.end--> <td class="fieldLabel" style="text-align: center; width: 63px;"><span style="color: red;">*</span>&nbsp;id</td> <td id="ID" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">项目名称</td> <td id="项目名称" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">装置名称</td> <td id="装置名称" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">Customer Name</td> <td id="CustomerName" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">E-NO/序列号</td> <td id="ENO" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">产品描述</td> <td id="产品描述" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">Remark</td> <td id="Remark" style="width: 254px;">&nbsp;</td> </tr> </tbody> </table> <!-- ==================== 查询结果表格容器 ==================== --> <div id="resultTableContainer" style="margin-top: 20px; padding: 0 10px;"><!-- 动态表格将插入到这里 --></div> <!-- ==================== 手机适配区域(可选)==================== --> <div class="slide4phone"> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder2" style="table-layout: fixed;"> <colgroup> <col width="460" /> <col width="140" /> <col /> </colgroup> </table> </div> <!-- ==================== 查询按钮 ==================== --> <div style="text-align: center; margin: 20px 0;">&nbsp; &nbsp; &nbsp; &nbsp; <input id="idslist" name="idslist" type="hidden" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong> <input id="check" name="check" onclick="clickData()" type="button" value="check个人记录" /> </strong></div> <!-- ==================== 移动端表格占位 ==================== --> <div class="slide4phone2" id="reptable"> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder2" style="width: 1300px;"> </table> </div> <script language="javascript"> function clickData() { // 获取输入框的值并添加通配符 % 用于模糊查询 var username = '%' + document.getElementById('username').textContent.trim() + '%'; var 项目name = '%' + document.getElementById('项目名称').textContent.trim() + '%'; var 装置name = '%' + document.getElementById('装置名称').textContent.trim() + '%'; var CustomerName = '%' + document.getElementById('CustomerName').textContent.trim() + '%'; var ENO = '%' + document.getElementById('ENO').textContent.trim() + '%'; var Remark = '%' + document.getElementById('Remark').textContent.trim() + '%'; var Product描述 = '%' + document.getElementById('产品描述').textContent.trim() + '%'; // 构建 SQL 查询语句(注意字段名和表名需正确) var sqlQuery2 = "SELECT Subject,Status,申请人,申请日期,产品类别,其他,type,内勤,系统,customer,ProjectName,DeviceName,新产品,[E-NO/序列号],目标价,数量,产地,含税报价,HandlerRemark,Remark,描述 " + "FROM X_BPM_DWH_819 " + "WHERE ProjectName LIKE '" + 项目name + "' " + "AND DeviceName LIKE '" + 装置name + "' " + "AND customer LIKE '" + CustomerName + "' " + "AND [E-NO/序列号] LIKE '" + ENO + "' " + "AND Remark LIKE '" + Remark + "' " + "AND 描述 LIKE '" + Product描述 + "'"; // 调用后台服务获取数据(假设返回的是二维数组) eval("var arr=" + service("common.js", "getDbsRecords", sqlQuery2, "array")); // 清空之前的结果 var container = document.getElementById("resultTableContainer"); container.innerHTML = ""; if (!arr || arr.length === 0) { container.innerHTML = "<p>未找到匹配的数据。</p>"; return; } // 创建表格 var table = document.createElement("table"); table.className = "tableListBorder"; table.style.width = "100%"; table.style.tableLayout = "fixed"; table.setAttribute("border", "1"); table.setAttribute("cellpadding", "5"); table.setAttribute("cellspacing", "0"); // 添加表头(使用 arr[0] 的键作为列名,或手动定义) var thead = document.createElement("thead"); var headerRow = document.createElement("tr"); // 手动定义表头文字(与 SELECT 字段顺序一致) var headers = [ "Subject", "Status", "申请人", "申请日期", "产品类别", "其他", "类型", "内勤", "系统", "客户", "项目名称", "装置名称", "新产品", "E-NO/序列号", "目标价", "数量", "产地", "含税报价", "处理备注", "备注", "描述" ]; headers.forEach(function (text) { var th = document.createElement("th"); th.style.backgroundColor = "#f0f0f0"; th.style.textAlign = "center"; th.style.fontSize = "14px"; th.textContent = text; headerRow.appendChild(th); }); thead.appendChild(headerRow); table.appendChild(thead); // 添加数据行 var tbody = document.createElement("tbody"); arr.forEach(function (row) { var tr = document.createElement("tr"); for (var i = 0; i < row.length; i++) { var td = document.createElement("td"); td.style.padding = "5px"; td.style.fontSize = "13px"; td.style.wordBreak = "break-word"; td.textContent = row[i] || ""; tr.appendChild(td); } tbody.appendChild(tr); }); table.appendChild(tbody); // 将表格插入容器 container.appendChild(table); } </script> 修改代码 第二次按查询按钮后 会重置生成结果
09-24
<!-- ==================== 主表单结构 ==================== --> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableForm" style="table-layout: fixed;"> <colgroup> <col width="80" /> <col /><!--hide4phone.start--> <col width="80" /> <col width="380" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td style="text-align: right;">&nbsp;<span style="color: red;">*</span>Subject:</td> <td dbf.type="required" id="dbf.subject">&nbsp;</td> <!--show4phone.start--> </tr> <tr><!--show4phone.end--> <td style="text-align: right;">&nbsp;Status:</td> <td><span id="mapping.dbf.procXSource">&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Responsor: <span id="mapping.dbf.responsorSource">&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Participants: <span id="mapping.dbf.participantsSource">&nbsp;</span></td> </tr> </tbody> </table> <div>&nbsp;</div> <!-- ==================== 页面标题 ==================== --> <div style="text-align: center;"> <h1><img src="../common/logo.png" /> [报价历史查询内勤用]</h1> </div> <div>[Design form here, based on the template below, or customized by yourself after the template removed]</div> <!-- ==================== 查询输入区域 ==================== --> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder" style="table-layout: fixed; width: 928px;"> <colgroup> <col width="130" /> <col /><!--hide4phone.start--> <col width="130" /> <col width="330" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td colspan="4" style="background-color: lightyellow;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center; width: 147px;"><span style="color: red;">*</span>&nbsp;username</td> <td id="username" style="width: 210px;">&nbsp;</td> <!--show4phone.start--> </tr> <tr><!--show4phone.end--> <td class="fieldLabel" style="text-align: center; width: 63px;"><span style="color: red;">*</span>&nbsp;id</td> <td id="ID" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">项目名称</td> <td id="项目名称" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">装置名称</td> <td id="装置名称" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">Customer Name</td> <td id="CustomerName" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">E-NO/序列号</td> <td id="ENO" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">产品描述</td> <td id="产品描述" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">Remark</td> <td id="Remark" style="width: 254px;">&nbsp;</td> </tr> </tbody> </table> <!-- ==================== 查询结果表格容器 ==================== --> <div id="resultTableContainer" style="margin-top: 20px; padding: 0 10px;"><!-- 动态表格将插入到这里 --></div> <!-- ==================== 手机适配区域(可选)==================== --> <div class="slide4phone"> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder2" style="table-layout: fixed;"> <colgroup> <col width="460" /> <col width="140" /> <col /> </colgroup> </table> </div> <!-- ==================== 查询按钮 ==================== --> <div style="text-align: center; margin: 20px 0;">&nbsp; &nbsp; &nbsp; &nbsp; <input id="idslist" name="idslist" type="hidden" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong> <input id="check" name="check" onclick="clickData()" type="button" value="check个人记录" /> </strong></div> <!-- ==================== 移动端表格占位 ==================== --> <div class="slide4phone2" id="reptable"> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder2" style="width: 1300px;"> </table> </div> <script language="javascript"> function clickData() { // === 1. 安全获取单元格文本(处理 &nbsp; 和空白)=== function getCellText(id) { var el = document.getElementById(id); if (!el) return ''; // 替换 &nbsp; 为普通空格,并 trim return (el.innerText || el.textContent || '') .replace(/[\u00A0\s]+/g, ' ') .trim(); } var username = '%' + getCellText('username') + '%'; var projectName = '%' + getCellText('项目名称') + '%'; var deviceName = '%' + getCellText('装置名称') + '%'; var customerName = '%' + getCellText('CustomerName') + '%'; var eno = '%' + getCellText('ENO') + '%'; var remark = '%' + getCellText('Remark') + '%'; var productDesc = '%' + getCellText('产品描述') + '%'; // === 2. 构建 SQL 查询语句 === var sqlQuery2 = "SELECT Subject,Status,申请人,申请日期,产品类别,其他,type,内勤,系统,customer,ProjectName,DeviceName,新产品,[E-NO/序列号],目标价,数量,产地,含税报价,HandlerRemark,Remark,描述 " + "FROM X_BPM_DWH_819 " + "WHERE ProjectName LIKE '" + projectName + "' " + "AND DeviceName LIKE '" + deviceName + "' " + "AND customer LIKE '" + customerName + "' " + "AND [E-NO/序列号] LIKE '" + eno + "' " + "AND Remark LIKE '" + remark + "' " + "AND 描述 LIKE '" + productDesc + "'"; // === 3. 调用后台服务获取数据(使用 try 防止崩溃)=== var arr = null; try { // 使用局部变量名防止冲突(避免 var arr 在全局污染) eval("var queryResultData = " + service("common.js", "getDbsRecords", sqlQuery2, "array") + ";"); arr = typeof queryResultData !== 'undefined' ? queryResultData : null; } catch (e) { console.error("查询执行失败:", e); alert("数据查询出错,请联系管理员。"); return; } // === 4. 清空容器(关键:必须强制移除所有子元素)=== var container = document.getElementById("resultTableContainer"); // 方法一:先移除所有子节点 while (container.firstChild) { container.removeChild(container.firstChild); } // 方法二:额外保险 —— 再设置 innerHTML 清理残留 container.innerHTML = ''; // 彻底清空 // === 5. 判断是否有数据 === if (!arr || !Array.isArray(arr) || arr.length === 0) { var noData = document.createElement('p'); noData.style.textAlign = 'center'; noData.style.color = '#999'; noData.style.fontStyle = 'italic'; noData.textContent = '未找到匹配的数据。'; container.appendChild(noData); return; } // === 6. 创建新表格 === var table = document.createElement('table'); table.className = 'tableListBorder'; table.style.width = '100%'; table.style.tableLayout = 'fixed'; table.setAttribute('border', '1'); table.setAttribute('cellpadding', '5'); table.setAttribute('cellspacing', '0'); // --- 表头 --- var thead = document.createElement('thead'); var headerRow = document.createElement('tr'); var headers = [ "Subject", "Status", "申请人", "申请日期", "产品类别", "其他", "类型", "内勤", "系统", "客户", "项目名称", "装置名称", "新产品", "E-NO/序列号", "目标价", "数量", "产地", "含税报价", "处理备注", "备注", "描述" ]; headers.forEach(function(text) { var th = document.createElement('th'); th.style.backgroundColor = '#f0f0f0'; th.style.textAlign = 'center'; th.style.fontSize = '14px'; th.style.padding = '8px'; th.textContent = text; headerRow.appendChild(th); }); thead.appendChild(headerRow); table.appendChild(thead); // --- 表体 --- var tbody = document.createElement('tbody'); arr.forEach(function(row) { if (!Array.isArray(row)) return; // 安全检查 var tr = document.createElement('tr'); row.forEach(function(cell) { var td = document.createElement('td'); td.style.padding = '6px'; td.style.fontSize = '13px'; td.style.wordBreak = 'break-word'; td.style.whiteSpace = 'normal'; td.textContent = cell == null ? '' : String(cell).trim(); }); tbody.appendChild(tr); }); table.appendChild(tbody); // === 7. 插入新表格(唯一一次 appendChild)=== container.appendChild(table); } </script> 结果都成空白了
最新发布
09-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值