sql常用查询语句

数据库   发布日期:2023年05月10日   浏览次数:426

count(*)

  1. $sql_page_xy="select count(*) from xy_tk_xueyuan where bianma_dw='".$vv_bianma_dw."' ";
  2. $result_page_xy=$mysqli->query($sql_page_xy);
  3. $rs_page_xy=$result_page_xy->fetch_array();
  4. $xueyuan_count=$rs_page_xy[0];

distinct取重复(此处不用group by可实现按组统计总数)

  1. $sql_page_cc="select count(distinct xingming) from xy_tk_xueyuan where bianma_dw='".$vv_bianma_dw."' and banji_id='".$rs["id"]."' ";
  2. $result_page_cc=$mysqli->query($sql_page_cc);
  3. $rs_page_cc=$result_page_cc->fetch_array();
  4. $chongming_count=$rs_page_cc[0];

SUM求和

  1. $sqlhg="select SUM(keshi) AS keshi from xy_dingding_keshi where bianma_dw='".$vv_bianma_dw."' and banji_id='".$banji_id."' and kecheng_name in (".$kaicheng_name_list.") ";
  2. $resulthg=$mysqli->query($sqlhg);
  3. if($rshg=$resulthg->fetch_assoc()){
  4. $jieye_keshi=$rshg["keshi"];
  5. }

order by field 字符串排序

  1. $dd_id=trim($dd_id,",");
  2. $dd_id="'".str_replace(",","','",$dd_id)."'";
  3. $sql="select kaishi_time from xy_dingding where bianma_dw='".$vv_bianma_dw."' and id in (".$dd_id.") order by field(id,".$dd_id.")";

like按匹配度倒序唯一查询结果

  1. $sqle="select * from xy_dingding_info where kecheng_name='".$e_kecheng_name."' and bianma_dw='".$vv_bianma_dw."' and (xingming like '%".$rs["xingming"]."%' or nicheng like '%".$rs["xingming"]."%') order by ((xingming like '%".$rs["xingming"]."%') + (nicheng like '%".$rs["xingming"]."%')  + (xingming like '%".$rs["tel"]."%')  + (nicheng like '%".$rs["tel"]."%') ) desc limit 1 ";

多条件求合

  1. 一次查询中,多个条件的数字分别统计
  2. $sql_2="select count(*) AS all_nums,SUM(CASE WHEN zhuangtai='未归还' THEN 1 ELSE 0 END) AS weiguihuan_num from dangxiao_zichan_jieyong_list where zhangtao='".$_SESSION['dangxiao_zc_zhangtao']."' and dh='".$rs["dh"]."' ";
  3. $result_2=$mysqli->query($sql_2);
  4. $rs_2=$result_2->fetch_array();
  5. $all_num=$rs_2["all_nums"];
  6. $weiguihuan_num=$rs_2["weiguihuan_num"];*/

嵌套查询

  1. $sql_zidian="select sfz from xy_tk_qiandao where banji_id='".$banji_id."' and (q_time='".$date1."') and sfz in (".$search_pid_id.") and ".$ziduan_v."<>'' "; 
  2. $result_zidian=$mysqli->query($sql_zidian);
  3. $pm_str='';
  4. while ($rs_zidian=$result_zidian->fetch_assoc()){
  5. $pm_str=$pm_str."'".$rs_zidian["sfz"]."',";
  6. }
  7. $search_pid_id=substr($pm_str,0,-1);
  8.  
  9. if ($search_pid_id==''){
  10. $search_pid_id="'xxx'";
  11. }
  12. $z_sql_str.=" and ( sfz in (".$search_pid_id.") ) ";


以上就是sql常用查询语句的详细内容,更多关于sql常用查询语句的资料请关注九品源码其它相关文章!