- 论坛徽章:
- 0
|
http://www.linux-ren.org/modules/newbb/viewtopic.php?post_id=35138&topic_id=6358
顺便附带一个笔记:
sql语句同结构多表联合查询,合并时如果选择字段太简单数据没有唯一确定行,会把相同记录合并的,如下示例多选几个字段能保证每行都不同就可得到正确结果
for(int i=sday;i0)stab+=" union ";
stab+="SELECT subscriber.tel_num, talk_dur, charge,cli, dnis,call_time FROM "+getdate(year,month,i);
stab+=" LEFT OUTER JOIN subscriber ON subscriber.tel_num = ";
stab+=getdate(year,month,i)+".card WHERE (subscriber.account_num = '"+val+"')";
//stab+=" WHERE ("+getdate(year,month,i)+".card = '"+val+"')";
}
}
if(stab.length()>0){
sql="SELECT tel_num,COUNT(talk_dur) AS times,(COUNT(talk_dur)-SUM((talk_dur + 2)/(talk_dur + 1)-1))";
sql+=" AS talks, SUM(talk_dur) AS talkdur, SUM((talk_dur + 59) / 60) AS talklen, ";
sql+="SUM(charge) AS sumcharge FROM ("+stab+") DERIVEDTBL GROUP BY tel_num order by tel_num";
}
-----
不同结构多表也可联合搜索某字段是否存在
sql="select company_worker_info.id,company_expert_info.id,company_monitor_info.id from ";
sql+="company_worker_info,company_expert_info ,company_monitor_info where (";
sql+="company_worker_info.workid='"+name+"' and company_worker_info.companyid='"+cid+"') or(";
sql+="company_expert_info.workid='"+name+"' and company_expert_info.companyid='"+cid+"') or(";
sql+="company_monitor_info.workid='"+name+"' and company_monitor_info.companyid='"+cid+"')";
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/11458/showart_673442.html |
|