如何将两条SQL语句合成一条

sql="select count(*) as hgs from Qc_V_MESMidCon where mnumber='2402014' and itemcode='138' AND ISNUMERIC(result)=1 and result>='99.2' and checkdate>='"&checkdate1&"' and checkdate<='"&checkdate2&"' "

sql_1="select count(*) as hgs from Qc_V_MESMidCon where mnumber='2402014' and itemcode='138' AND ISNUMERIC(result)=1)and checkdate>='"&checkdate1&"' and checkdate<='"&checkdate2&"' "

如何将这两条SQL语句合成一条来写?
<%
Set rs = Server.CreateObject("ADODB.Recordset")
sql="select count(*) as hgs from Qc_V_MESMidCon where mnumber='2402014' and itemcode='138' AND ISNUMERIC(result)=1 and result>='99.2' and checkdate>='"&checkdate1&"' and checkdate<='"&checkdate2&"' "
rs.open sql ,conn,1,3
if Not rs.EOF then
hgs=rs("hgs")
end if
rs.close
sql_1="select count(*) as jls from Qc_V_MESMidCon where mnumber='2402014' and itemcode='138' AND ISNUMERIC(result)=1 and checkdate>='"&checkdate1&"' and checkdate<='"&checkdate2&"' "
rs.open sql_1 ,conn,1,3
if Not rs.EOF then
jls=rs("jls")
end if
rs.close
%>
合格率:<td ><%=Round((hgs/jls)*100,2)&"%"%></td>
想从以上的两个SQL语句中取出的数据用来做除法,但总是提示有错!!但又不知道错在哪里?

select count(*) as hgs from Qc_V_MESMidCon where mnumber='2402014' and itemcode='138' AND ISNUMERIC(result)=1 and result>='99.2' and checkdate>='"&checkdate1&"' and checkdate<='"&checkdate2&"'

union all

select count(*) as hgs from Qc_V_MESMidCon where mnumber='2402014' and itemcode='138' AND ISNUMERIC(result)=1)and checkdate>='"&checkdate1&"' and checkdate<='"&checkdate2&"'

得到两个记录的结果集。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-06-19
取交集的话,就用上面的就可以~
去并集的话,就用下面的~
第2个回答  2009-06-19
结果不一样呀?能合成一个?
string sql=select count(*) as hgs from Qc_V_MESMidCon where mnumber='2402014' and itemcode='138' AND ISNUMERIC(result)=1)and checkdate>='"&checkdate1&"' and checkdate<='"&checkdate2&"' "

用到 and result>='99.2' 的时候在后面拼一段吧。
关注这个问题,一起学习。
第3个回答  2009-06-19
你的需求是根据条件判断是不是需要加入result>='99.2' 这个条件吧 需求写明确来 问题很好解决 搞好了找我 根据条件改where 条件是可以用一条sql实现的
研究了好久才想到
相似回答