1. <?php
  2.  
  3. include_once "DBHelper.php";
  4. define('HOST', '127.0.0.1');
  5. define('USER', 'root');
  6. define('PASSWORD', '');
  7. define('DBNAME', 'sxbcms');
  8.  
  9. /*define('HOST', '120.25.144.215');
  10. define('USER', 'root');
  11. define('PASSWORD', '88ac86754a');
  12. define('DBNAME', 'sxbcms');*/
  13.  
  14. class student extends DBHelper{
  15.  
  16. function __construct()
  17. {
  18. parent::__construct();
  19. }
  20.  
  21. public function students(){
  22. $sql = "select user.id , user.mobile ,name, sex, user.birthday ,area, user.email, `fgraduate` from hr_users user left join hr_student on user.id = hr_student.user_id where user.usertype =1 ";
  23. $ret = $this->find($sql);
  24. return $ret;
  25. }
  26.  
  27. //获取最高的学位 limit 1
  28. public function edu($user_id){
  29. $sql = "select `user_id`, `fdegree`, `grade` , `fschool` , `fmajor` from hr_student_eduexp where user_id = $user_id order by fdegree desc limit 1";
  30. //echo $sql;
  31. $ret = $this->find($sql);
  32. return $ret;
  33. }
  34.  
  35. public function citys(&$data){
  36. $sql ="select * from hr_city ";
  37. $ret = $this->find($sql);
  38. $temp = [] ;
  39. foreach ($ret as $key => $value) {
  40. $temp[$value['id']] = $value['cityname'];
  41. }
  42. foreach($data as $key=>$value){
  43. $city = $value['area'];//获取area 的id
  44. if(isset($temp[$city]))
  45. {
  46. $data[$key]['citys']=$temp[$city];
  47. unset($data[$key]['city']);
  48. }
  49. }
  50. }
  51.  
  52. public function csv($file,$list)
  53. {
  54. $fh = fopen($file,'w') or die("Can't open file.csv");
  55.  
  56. foreach ($list as $sales_line) {
  57. if (fputcsv($fh, $sales_line) === false) {
  58. die("Can't write CSV line");
  59. }
  60. }
  61. fclose($fh) or die("Can't close file.csv");
  62.  
  63. }
  64. }
  65.  
  66. $stu = new student();
  67. $listStu = $stu->students();
  68. foreach ($listStu as $key => $value) {
  69. $user_id = $value['id'];//获取用户的id
  70. if(!empty($user_id)){
  71. $eduStu = $stu->edu($user_id);
  72. /*echo "<pre>";print_r($eduStu);echo "</pre>";*/
  73. //根据user_id遍历教育经历,过滤掉为空的教育经历
  74. if(is_array($eduStu)){
  75. foreach ($eduStu as $edu) {
  76. $listStu[$key]['fdegree'] = $edu['fdegree'];
  77. $listStu[$key]['grade'] = $edu['grade'];
  78. $listStu[$key]['fschool'] = $edu['fschool'];
  79. $listStu[$key]['fmajor'] = $edu['fmajor'];
  80. }
  81. }
  82. }
  83. }
  84.  
  85. $stu->citys($listStu);
  86. $stu->csv("student123.csv", $listStu);
  87.  
  88. ?>

DBhelper.php

  1. <?php
  2. header("Content-type:text/html;charset=utf-8");
  3.  
  4. class DBHelper{
  5.  
  6. protected $conn;
  7.  
  8. function __construct()
  9. {
  10.  
  11. $this->InitConn();
  12.  
  13. }
  14.  
  15. private function InitConn()
  16. {
  17.  
  18. $this->conn = new mysqli(HOST, USER, PASSWORD, DBNAME);
  19.  
  20. if ($this->conn->connect_errno) {
  21.  
  22. printf("Connect failed: %s\n", $this->conn->connect_error);
  23.  
  24. exit();
  25.  
  26. }
  27.  
  28. $this->conn->query("set names utf8");
  29.  
  30. }
  31.  
  32. public function changeDb($user, $pass, $ip, $db)
  33. {
  34.  
  35. $this->conn = new mysqli($ip, $user, $pass, $db);
  36.  
  37. if ($this->conn->connect_errno) {
  38.  
  39. printf("Connect failed: %s\n", $this->conn->connect_error);
  40.  
  41. exit();
  42.  
  43. }
  44.  
  45. $this->conn->query("set names utf8");
  46.  
  47. }
  48.  
  49. public function Execute($sql)
  50. {
  51. echo $sql."\n\n";
  52. return $this->conn->query($sql);
  53.  
  54. }
  55.  
  56. public function find($sql)
  57. {
  58.  
  59. $ret = [];
  60.  
  61. $list = $this->conn->query($sql);
  62.  
  63. while ($row = $list->fetch_array(MYSQLI_ASSOC)) {
  64.  
  65. $ret[] = $row;
  66.  
  67. }
  68.  
  69. return $ret;
  70.  
  71. }
  72.  
  73. public function findOne($sql)
  74. {
  75.  
  76. $list = $this->conn->query($sql);
  77.  
  78. $row = $list->fetch_array(MYSQLI_ASSOC);
  79.  
  80. return $row;
  81.  
  82. }
  83.  
  84. public function Save($table ,$data)
  85. {
  86. $sql ="insert into $table set ";
  87. foreach ($data as $key => $value) {
  88. $data[$key] = addslashes($value);
  89. }
  90. foreach ($data as $key => $value) {
  91. $sql =$sql ."`".$key."`='". $value ."',";
  92. }
  93. $sql = substr($sql,,count($sql)-);
  94. echo $sql."\n";
  95. return $this->conn->query($sql);
  96. }
  97.  
  98. public function Close()
  99. {
  100.  
  101. $this->conn->close();
  102.  
  103. }
  104.  
  105. public function executesql($sql)
  106. {
  107. return $this->conn->query($sql);
  108. }
  109.  
  110. }

举例子

  1. <?php
  2.  
  3. include_once "DBHelper.php";
  4. define('HOST', '120.25.144.215');
  5. define('USER', 'root');
  6. define('PASSWORD', '88ac86754a');
  7. define('DBNAME', 'sxbcms');
  8.  
  9. /*
  10. 自己注册企业列表
  11.  
  12. */
  13.  
  14. /**
  15. *
  16. */
  17. class enterprise extends DBHelper
  18. {
  19.  
  20. function __construct()
  21. {
  22. parent::__construct();
  23. }
  24.  
  25. public function listEnt()
  26. {
  27. $sql = "select hr_company.id , comfullname ,email ,mobile ,scale ,industry,mainproduct,enterprise ,hr_users.id as user_id from hr_company left join hr_users on hr_company.user_id = hr_users.id
  28. where user_id not in (select user_id from hr_edit_create_company)";
  29.  
  30. $ret = $this->find($sql);
  31. return $ret;
  32. }
  33.  
  34. /*
  35. 发不过的职位总数
  36.  
  37. */
  38.  
  39. public function jobs()
  40. {
  41. $sql ="select count(*) as ct ,user_id from hr_job where addtime > '2016-01-01' and addtime < '2016-09-30' group by user_id" ;
  42. $ret = $this->find($sql);
  43. $temp =[];
  44. foreach ($ret as $key => $value) {
  45. $uid = $value['user_id'];
  46. $temp[$uid] = $value['ct'];
  47. }
  48.  
  49. return $temp;
  50.  
  51. }
  52.  
  53. public function citys(&$data)
  54. {
  55. $sql ="select * from hr_city ";
  56. $ret = $this->find($sql);
  57. $temp = [] ;
  58. foreach ($ret as $key => $value) {
  59. $temp[$value['id']] = $value['cityname'];
  60. }
  61. foreach($data as $key=>$value){
  62.  
  63. $provinces = $value['provinces'];
  64. $city = $value['city'];
  65. if(isset($temp[$provinces]))
  66. {
  67. $data[$key]['pro']=$temp[$provinces];
  68. $data[$key]['citys']=$temp[$city];
  69. unset($data[$key]['provinces']);
  70. unset($data[$key]['city']);
  71. }
  72.  
  73. }
  74.  
  75. }
  76.  
  77. //设置行业
  78. public function injertindustry(&$data)
  79. {
  80. $sql ="select * from hr_industry";
  81. $ret = $this->find($sql);
  82. $temp = [] ;
  83. foreach ($ret as $key => $value) {
  84. $temp[$value['id']] = $value['name'];
  85. }
  86. foreach ($data as $key => $value) {
  87. $industry =$value['industry'];
  88.  
  89. if(isset($temp[$industry])){
  90. $name =$temp[$industry];
  91. $data[$key]['industry_name'] = $name;
  92. }else
  93. {
  94. $data[$key]['industry_name'] ="";
  95.  
  96. }
  97. }
  98. }
  99.  
  100. public function Position(&$data)
  101. {
  102. $sql ="select * from hr_position";
  103. $ret = $this->find($sql);
  104. $temp = [] ;
  105. foreach ($ret as $key => $value) {
  106. $temp[$value['id']] = $value['name'];
  107. }
  108. foreach ($data as $key => $value) {
  109. $jobtype =$value['jobtype'];
  110.  
  111. if(isset($temp[$jobtype])){
  112. $name =$temp[$jobtype];
  113. $data[$key]['jobtype'] = $name;
  114. }else
  115. {
  116. $data[$key]['jobtype'] ="";
  117.  
  118. }
  119. }
  120.  
  121. }
  122.  
  123. /*
  124. 职位的投递量
  125. */
  126. public function listJobBYCompany()
  127. {
  128. //获取全部的job-id和对应的company
  129. $sql = "select user_id ,id from hr_job";
  130. $ret = $this->find($sql);
  131.  
  132. $sql = "select count(*) , jobid from hr_action group by jobid ";
  133. $list = $this->find($sql);
  134.  
  135. $temp =[];
  136. foreach ($list as $key => $value) {
  137. $jobid =$value['jobid'];
  138. $temp[$jobid] =$value;
  139. }
  140.  
  141. foreach ($ret as $key => $value) {
  142. $jobid =$value['id'];
  143. $ret[$key]['total'] =$temp[$jobid];
  144. }
  145.  
  146. return $temp;
  147.  
  148. }
  149.  
  150. public function listContainJob($user_id,$email=null ,$name=null)
  151. {
  152. $sql = "select id , title,num ,addtime ,provinces ,city,jobtype ,viewnum from hr_job where user_id=".$user_id;
  153. $list = $this->find($sql);
  154. foreach ($list as $key => $value) {
  155. $jobid =$value["id"];
  156. $sql = "select count(*) as ct from hr_action where jobid=".$jobid;
  157. $ct = $this->findOne($sql);
  158. $list[$key]['deliver'] = $ct['ct'];
  159. $list[$key]['email'] = $email;
  160. $list[$key]['name'] = $name;
  161. $sql = "select count(*) as ct from hr_action where jobid=".$jobid ." and open_viwe = 1";
  162. $ct = $this->findOne($sql);
  163. $list[$key]['resumeviewn'] = $ct['ct'];
  164. }
  165. return $list;
  166. }
  167.  
  168. public function csv($file,$list)
  169. {
  170. $fh = fopen($file,'w') or die("Can't open file.csv");
  171.  
  172. foreach ($list as $sales_line) {
  173. if (fputcsv($fh, $sales_line) === false) {
  174. die("Can't write CSV line");
  175. }
  176. }
  177. fclose($fh) or die("Can't close file.csv");
  178.  
  179. }
  180.  
  181. }
  182.  
  183. $ent= new enterprise();
  184. $list = $ent->listEnt();
  185. $jobs =$ent->jobs();
  186. foreach ($list as $key => $value) {
  187. $uid = $value['user_id'];
  188. if(isset($jobs[$uid])){
  189. $list[$key]['total'] = $jobs[$uid];
  190. }else
  191. {
  192. $list[$key]['total'] = ;
  193. }
  194.  
  195. }
  196. $ent->injertindustry($list);
  197. $jobs =[];
  198. foreach ($list as $key => $value) {
  199. $uid = $value['user_id'];
  200. if(intval($uid)>){
  201. $email =$value['email'];
  202. $name =$value['comfullname'];
  203. $li = $ent->listContainJob($uid,$email ,$name);
  204. if(!empty($li))
  205. {
  206. foreach($li as $item)
  207. {
  208. array_push($jobs, $item);
  209. }
  210. }
  211. }
  212.  
  213. }
  214.  
  215. $ent->citys($jobs);
  216. $ent->Position($jobs);
  217. $ent->csv("ent.csv", $list);
  218. $ent->csv("jobs.csv", $jobs);

php实现多表(四表)连接的更多相关文章

  1. 数据库SQL Server2012笔记(四)——多表查询、子查询、分页查询、用查询结果创建新表和外连接

    1.多表查询 1)笛卡尔集: select  *  from  表名1,表名2 select  *  from  表名1.表名2  where   表名1.字段名=表名2.字段名 注: 若有两张表有同 ...

  2. iptable四表五链

    链(内置): PREROUTING:对数据包作路由选择前应用此链中的规则: INPUT:进来的数据包应用此规则链中的策略: FORWARD:转发数据包时应用此规则链中的策略: OUTPUT:外出的数据 ...

  3. iptables四表五链及默认规则使用,

    网络基础 TCP/IP模型: 应用层===传输层===网络层===数据链里层===物理层 数据封装: MAC帧头+IP报头+TCP/UDP报头===HTTP请求 数据帧 TCP/UDP报头: 随机产生 ...

  4. iptables详解(2):四表五链

    关于iptables中“四表五链”,我们今天来好好唠唠: 1.表的概念: 我们把具有相同功能的规则的集合叫做"表",所以说,不同功能的规则,我们可以放置在不同的表中进行管理,而ip ...

  5. 1、架构--架构图、Iptables(简介、四表五链、流程图、使用、扩展模块)、包过滤防火墙

    笔记 1.画架构图 2.Iptables 1.1 什么是防火墙 防止别人恶意访问. 1.2 防火墙种类 硬件防火墙 F5 软件防火墙 iptables firewalld 安全组 3.Iptables ...

  6. 浅谈Oracle表之间各种连接

    Oracle表之间的连接分为三种: 1.内连接(自然连接) 2.外连接 2.1.左外连接(左边的表不加限制,查询出全部满足条件的结果) 2.2.右外连接(右边的表不加限制,查询出全部满足条件的结果) ...

  7. 无废话ExtJs 入门教程四[表单:FormPanel]

    无废话ExtJs 入门教程四[表单:FormPanel] extjs技术交流,欢迎加群(201926085) 继上一节内容,我们在窗体里加了个表单.如下所示代码区的第28行位置,items:form. ...

  8. Oracle 表三种连接方式(sql优化)

    在查看sql执行计划时,我们会发现表的连接方式有多种,本文对表的连接方式进行介绍以便更好看懂执行计划和理解sql执行原理. 一.连接方式: 嵌套循环(Nested Loops (NL)) (散列)哈希 ...

  9. hadoop大数据处理之表与表的连接

    hadoop大数据处理之表与表的连接 前言:  hadoop中表连接其实类似于我们用sqlserver对数据进行跨表查询时运用的inner join一样,两个连接的数据要有关系连接起来,中间必须有一个 ...

  10. Oracle数据库(三)表操作,连接查询,分页

    复制表 --复制表 create table new_table as select * from Product --复制表结构不要数据 在where后面跟一个不成立的条件,就会仅复制表的结构而不复 ...

随机推荐

  1. 开发网站相关知识html和javascript

    1.html 布局 https://github.com/bramstein/jlayout/ http://welcome.totheinter.net/columnizer-jquery-plug ...

  2. js禁止中文输入 最简洁的【禁止输入中文】

    方法一:禁止中文输入法 <input type="text"  > 方法二:禁止黏贴,禁止拖拽,禁止中文输入法! 这种方法是最强的禁止 中文输入 <input t ...

  3. bash 变量使用技巧

  4. html5 新增语义标签

    一份模板: <body> <header> <hgroup> <h1>Page title</h1> <h2>Page subt ...

  5. STM32F103控制两个步进电机按照一定转速比运动

    这个暑假没有回家,在学校准备九月份的电子设计竞赛.今天想给大家分享一下STM32定时器控制两个步进电机按照一定速度比转动的问题. 这次做的05年的电子设计竞赛题目,运动悬挂系统..本实验是控制两个步进 ...

  6. css案例学习之float浮动

    代码: <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  7. Noip2013之路

    当我回望过去的一年,我想,我对自己没有任何的愧疚,因为我每一个脚印,都踩的很坚实. 第一次参加模拟赛,第一次接触NOIP的规则,虽然考得不是特别好,但是还是很有收获的,首先,数组一定要开得足够大,不然 ...

  8. hdu 5093 Battle ships 匈牙利 很巧妙的建图思路

    //这题逼我把匈牙利学了 之前一直很勤快敲网络流 而且不以为耻反以为荣 解:首先按行扫描编号,如果在同一块中(即可以相互攻击),那么将其标为相同的数组,对列也做同样的操作. 然后扫描整张图,如果行编号 ...

  9. linux命令之mv

    linux下的mv即move的意思 该命令的一般形式: mv [选项] 参数1 参数2 选项: -b                如果已存在相同文件名,则覆盖前进行备份 -f             ...

  10. C链表之创建简单静态链表

    C代码: #include<stdio.h> #include<stdlib.h> #include<malloc.h> //创建简单静态链表 typedef st ...