php实现多表(四表)连接
<?php include_once "DBHelper.php";
define('HOST', '127.0.0.1');
define('USER', 'root');
define('PASSWORD', '');
define('DBNAME', 'sxbcms'); /*define('HOST', '120.25.144.215');
define('USER', 'root');
define('PASSWORD', '88ac86754a');
define('DBNAME', 'sxbcms');*/ class student extends DBHelper{ function __construct()
{
parent::__construct();
} public function students(){
$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 ";
$ret = $this->find($sql);
return $ret;
} //获取最高的学位 limit 1
public function edu($user_id){
$sql = "select `user_id`, `fdegree`, `grade` , `fschool` , `fmajor` from hr_student_eduexp where user_id = $user_id order by fdegree desc limit 1";
//echo $sql;
$ret = $this->find($sql);
return $ret;
} public function citys(&$data){
$sql ="select * from hr_city ";
$ret = $this->find($sql);
$temp = [] ;
foreach ($ret as $key => $value) {
$temp[$value['id']] = $value['cityname'];
}
foreach($data as $key=>$value){
$city = $value['area'];//获取area 的id
if(isset($temp[$city]))
{
$data[$key]['citys']=$temp[$city];
unset($data[$key]['city']);
}
}
} public function csv($file,$list)
{
$fh = fopen($file,'w') or die("Can't open file.csv"); foreach ($list as $sales_line) {
if (fputcsv($fh, $sales_line) === false) {
die("Can't write CSV line");
}
}
fclose($fh) or die("Can't close file.csv"); }
} $stu = new student();
$listStu = $stu->students();
foreach ($listStu as $key => $value) {
$user_id = $value['id'];//获取用户的id
if(!empty($user_id)){
$eduStu = $stu->edu($user_id);
/*echo "<pre>";print_r($eduStu);echo "</pre>";*/
//根据user_id遍历教育经历,过滤掉为空的教育经历
if(is_array($eduStu)){
foreach ($eduStu as $edu) {
$listStu[$key]['fdegree'] = $edu['fdegree'];
$listStu[$key]['grade'] = $edu['grade'];
$listStu[$key]['fschool'] = $edu['fschool'];
$listStu[$key]['fmajor'] = $edu['fmajor'];
}
}
}
} $stu->citys($listStu);
$stu->csv("student123.csv", $listStu); ?>
DBhelper.php
<?php
header("Content-type:text/html;charset=utf-8"); class DBHelper{ protected $conn; function __construct()
{ $this->InitConn(); } private function InitConn()
{ $this->conn = new mysqli(HOST, USER, PASSWORD, DBNAME); if ($this->conn->connect_errno) { printf("Connect failed: %s\n", $this->conn->connect_error); exit(); } $this->conn->query("set names utf8"); } public function changeDb($user, $pass, $ip, $db)
{ $this->conn = new mysqli($ip, $user, $pass, $db); if ($this->conn->connect_errno) { printf("Connect failed: %s\n", $this->conn->connect_error); exit(); } $this->conn->query("set names utf8"); } public function Execute($sql)
{
echo $sql."\n\n";
return $this->conn->query($sql); } public function find($sql)
{ $ret = []; $list = $this->conn->query($sql); while ($row = $list->fetch_array(MYSQLI_ASSOC)) { $ret[] = $row; } return $ret; } public function findOne($sql)
{ $list = $this->conn->query($sql); $row = $list->fetch_array(MYSQLI_ASSOC); return $row; } public function Save($table ,$data)
{
$sql ="insert into $table set ";
foreach ($data as $key => $value) {
$data[$key] = addslashes($value);
}
foreach ($data as $key => $value) {
$sql =$sql ."`".$key."`='". $value ."',";
}
$sql = substr($sql,,count($sql)-);
echo $sql."\n";
return $this->conn->query($sql);
} public function Close()
{ $this->conn->close(); } public function executesql($sql)
{
return $this->conn->query($sql);
} }
举例子
<?php include_once "DBHelper.php";
define('HOST', '120.25.144.215');
define('USER', 'root');
define('PASSWORD', '88ac86754a');
define('DBNAME', 'sxbcms'); /*
自己注册企业列表 */ /**
*
*/
class enterprise extends DBHelper
{ function __construct()
{
parent::__construct();
} public function listEnt()
{
$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
where user_id not in (select user_id from hr_edit_create_company)"; $ret = $this->find($sql);
return $ret;
} /*
发不过的职位总数 */ public function jobs()
{
$sql ="select count(*) as ct ,user_id from hr_job where addtime > '2016-01-01' and addtime < '2016-09-30' group by user_id" ;
$ret = $this->find($sql);
$temp =[];
foreach ($ret as $key => $value) {
$uid = $value['user_id'];
$temp[$uid] = $value['ct'];
} return $temp; } public function citys(&$data)
{
$sql ="select * from hr_city ";
$ret = $this->find($sql);
$temp = [] ;
foreach ($ret as $key => $value) {
$temp[$value['id']] = $value['cityname'];
}
foreach($data as $key=>$value){ $provinces = $value['provinces'];
$city = $value['city'];
if(isset($temp[$provinces]))
{
$data[$key]['pro']=$temp[$provinces];
$data[$key]['citys']=$temp[$city];
unset($data[$key]['provinces']);
unset($data[$key]['city']);
} } } //设置行业
public function injertindustry(&$data)
{
$sql ="select * from hr_industry";
$ret = $this->find($sql);
$temp = [] ;
foreach ($ret as $key => $value) {
$temp[$value['id']] = $value['name'];
}
foreach ($data as $key => $value) {
$industry =$value['industry']; if(isset($temp[$industry])){
$name =$temp[$industry];
$data[$key]['industry_name'] = $name;
}else
{
$data[$key]['industry_name'] =""; }
}
} public function Position(&$data)
{
$sql ="select * from hr_position";
$ret = $this->find($sql);
$temp = [] ;
foreach ($ret as $key => $value) {
$temp[$value['id']] = $value['name'];
}
foreach ($data as $key => $value) {
$jobtype =$value['jobtype']; if(isset($temp[$jobtype])){
$name =$temp[$jobtype];
$data[$key]['jobtype'] = $name;
}else
{
$data[$key]['jobtype'] =""; }
} } /*
职位的投递量
*/
public function listJobBYCompany()
{
//获取全部的job-id和对应的company
$sql = "select user_id ,id from hr_job";
$ret = $this->find($sql); $sql = "select count(*) , jobid from hr_action group by jobid ";
$list = $this->find($sql); $temp =[];
foreach ($list as $key => $value) {
$jobid =$value['jobid'];
$temp[$jobid] =$value;
} foreach ($ret as $key => $value) {
$jobid =$value['id'];
$ret[$key]['total'] =$temp[$jobid];
} return $temp; } public function listContainJob($user_id,$email=null ,$name=null)
{
$sql = "select id , title,num ,addtime ,provinces ,city,jobtype ,viewnum from hr_job where user_id=".$user_id;
$list = $this->find($sql);
foreach ($list as $key => $value) {
$jobid =$value["id"];
$sql = "select count(*) as ct from hr_action where jobid=".$jobid;
$ct = $this->findOne($sql);
$list[$key]['deliver'] = $ct['ct'];
$list[$key]['email'] = $email;
$list[$key]['name'] = $name;
$sql = "select count(*) as ct from hr_action where jobid=".$jobid ." and open_viwe = 1";
$ct = $this->findOne($sql);
$list[$key]['resumeviewn'] = $ct['ct'];
}
return $list;
} public function csv($file,$list)
{
$fh = fopen($file,'w') or die("Can't open file.csv"); foreach ($list as $sales_line) {
if (fputcsv($fh, $sales_line) === false) {
die("Can't write CSV line");
}
}
fclose($fh) or die("Can't close file.csv"); } } $ent= new enterprise();
$list = $ent->listEnt();
$jobs =$ent->jobs();
foreach ($list as $key => $value) {
$uid = $value['user_id'];
if(isset($jobs[$uid])){
$list[$key]['total'] = $jobs[$uid];
}else
{
$list[$key]['total'] = ;
} }
$ent->injertindustry($list);
$jobs =[];
foreach ($list as $key => $value) {
$uid = $value['user_id'];
if(intval($uid)>){
$email =$value['email'];
$name =$value['comfullname'];
$li = $ent->listContainJob($uid,$email ,$name);
if(!empty($li))
{
foreach($li as $item)
{
array_push($jobs, $item);
}
}
} } $ent->citys($jobs);
$ent->Position($jobs);
$ent->csv("ent.csv", $list);
$ent->csv("jobs.csv", $jobs);
php实现多表(四表)连接的更多相关文章
- 数据库SQL Server2012笔记(四)——多表查询、子查询、分页查询、用查询结果创建新表和外连接
1.多表查询 1)笛卡尔集: select * from 表名1,表名2 select * from 表名1.表名2 where 表名1.字段名=表名2.字段名 注: 若有两张表有同 ...
- iptable四表五链
链(内置): PREROUTING:对数据包作路由选择前应用此链中的规则: INPUT:进来的数据包应用此规则链中的策略: FORWARD:转发数据包时应用此规则链中的策略: OUTPUT:外出的数据 ...
- iptables四表五链及默认规则使用,
网络基础 TCP/IP模型: 应用层===传输层===网络层===数据链里层===物理层 数据封装: MAC帧头+IP报头+TCP/UDP报头===HTTP请求 数据帧 TCP/UDP报头: 随机产生 ...
- iptables详解(2):四表五链
关于iptables中“四表五链”,我们今天来好好唠唠: 1.表的概念: 我们把具有相同功能的规则的集合叫做"表",所以说,不同功能的规则,我们可以放置在不同的表中进行管理,而ip ...
- 1、架构--架构图、Iptables(简介、四表五链、流程图、使用、扩展模块)、包过滤防火墙
笔记 1.画架构图 2.Iptables 1.1 什么是防火墙 防止别人恶意访问. 1.2 防火墙种类 硬件防火墙 F5 软件防火墙 iptables firewalld 安全组 3.Iptables ...
- 浅谈Oracle表之间各种连接
Oracle表之间的连接分为三种: 1.内连接(自然连接) 2.外连接 2.1.左外连接(左边的表不加限制,查询出全部满足条件的结果) 2.2.右外连接(右边的表不加限制,查询出全部满足条件的结果) ...
- 无废话ExtJs 入门教程四[表单:FormPanel]
无废话ExtJs 入门教程四[表单:FormPanel] extjs技术交流,欢迎加群(201926085) 继上一节内容,我们在窗体里加了个表单.如下所示代码区的第28行位置,items:form. ...
- Oracle 表三种连接方式(sql优化)
在查看sql执行计划时,我们会发现表的连接方式有多种,本文对表的连接方式进行介绍以便更好看懂执行计划和理解sql执行原理. 一.连接方式: 嵌套循环(Nested Loops (NL)) (散列)哈希 ...
- hadoop大数据处理之表与表的连接
hadoop大数据处理之表与表的连接 前言: hadoop中表连接其实类似于我们用sqlserver对数据进行跨表查询时运用的inner join一样,两个连接的数据要有关系连接起来,中间必须有一个 ...
- Oracle数据库(三)表操作,连接查询,分页
复制表 --复制表 create table new_table as select * from Product --复制表结构不要数据 在where后面跟一个不成立的条件,就会仅复制表的结构而不复 ...
随机推荐
- 003 assert简述
- js new Date()
1.Date 对象用于处理日期和时间.创建 Date 对象的语法:var myDate=new Date()Date 对象会自动把当前日期和时间保存为其初始值.2.参数形式有以下5种: new Dat ...
- Windows下Vundle安装
鼠标手老是发作,没办法.想学习vim尽量减少编码时使用鼠标的频率.安装好gVim开始安装Vundle插件,总结下安装过程和各种遇到的坑: github上VundleVim倒是有说明 Windows S ...
- 菜单之二:使用xml文件定义菜单
参考<疯狂android讲义>2.10节 P174,参见归档project:XmlMenuDemo.zip 一般推荐使用XML文件定义菜单. 基本步骤如下: 1.定义布局文件 为简单显示原 ...
- PLSQL实例(游标)
在PLSQL块中执行SQL语句 A. 数据定义DDL: create,drop,truncate,不能直接执行,truncate执行时只做数据删除,不写日起,不维护索引 在PLSQL块中执行字符串 ...
- HTML5的结构学习(3) --- 综合运用
前面学习了HTML5新增的主体结构元素和新增的非主体结构元素, 而这里我们来学习如何去综合的运用这些新增元素. 1.大纲 HMTL5元素的关键就是将显示内容和便签类型紧密相关,提高了代码的语义化和可读 ...
- 初学swift笔记 方法(九)
import Foundation /* 方法 */ //实例方法 一定需要依附于对象 class MyPoint { var x: Double = 0.0 var y: Double = 0.0 ...
- SQL Server 查看备份集元数据的 4 种方法。
方法 1. restore labelonly 方法 2. restore headeronly 方法 3. restore filelistonly 方法 4. restore verifyonly ...
- kvm-GT
REF: http://los-vmm.sc.intel.com/wiki/How-to-setup-kvmgthttp://xenvgt.sh.intel.com/image/bdw-h/ Host ...
- Ruiy classicsQuotations
1,IT界,许多人会称自己为菜鸟,而每只菜鸟都会有鹰的梦想; 2,把做十件事精力用来做一件事情,你事业就经典了;