当前位置:网站首页>PHP高级开发案例(1):使用MYSQL语句跨表查询无法导出全部记录的解决方案
PHP高级开发案例(1):使用MYSQL语句跨表查询无法导出全部记录的解决方案
2022-08-04 03:28:00 【漏刻有时】
在实际案例开发过程中,使用mysql跨表查询语句来导出数据库内容。但是经过比对,总是发现导出的记录少于实际的记录。经查发现:用户表的部分用户被删除,导致无法正常读取而缺少导出记录的。
原代码
//$sql = "select a.pro_id,a.user_id,a.pro_serial,a.pro_name,a.pro_category,a.smallCategory,a.threeCategory,a.user_city,a.user_area,a.pro_declarant,b.user_id,b.user_phone,c.pro_id,c.leader_name,c.leader_phone,(CASE a.pro_audit when 0 then '未审核' when 2 then '区县审核' when 3 then '市级审核' when 4 then '省级审核' when 5 then '审核完成' when -1 then '退回' END) AS pro_audit,(CASE a.user_surveyor_type when 2 then '县属' when 3 then '市属' when 4 then '省属' END) AS user_surveyor_type,d.pro_id,d.agency_name FROM " . $db->table('project') . " AS a, " . $db->table('user') . " AS b," . $db->table('leader') . " AS c";//", " . $db->table('agency') . " AS d ";
$sql = "select a.pro_id,a.user_id,a.pro_serial,a.pro_name,a.pro_category,a.smallCategory,a.threeCategory,a.user_city,a.user_area,a.pro_declarant,b.user_id,b.user_phone,c.pro_id,c.leader_name,c.leader_phone,(CASE a.pro_audit when 0 then '未审核' when 2 then '区县审核' when 3 then '市级审核' when 4 then '省级审核' when 5 then '审核完成' when -1 then '退回' END) AS pro_audit,(CASE a.user_surveyor_type when 2 then '县属' when 3 then '市属' when 4 then '省属' END) AS user_surveyor_type FROM " . $db->table('project') . " AS a, " . $db->table('user') . " AS b," . $db->table('leader') . " AS c";//", " . $db->table('agency') . " AS d ";
$sql .= " WHERE a.user_id = b.user_id AND a.pro_id = c.pro_id";// AND a.pro_id = d.pro_ida.pro_audit <> 0 AND
$sql .= " ORDER BY a.pro_id DESC";
$row = $db->queryall($sql);
$infoName = "项目信息表" . date("Y.m.d");
解决方案
case "exp";
require 'libs/project.export.php';
$sql = "select pro_id,user_id,pro_serial,pro_name,pro_category,smallCategory,threeCategory,user_city,user_area,pro_declarant,(CASE pro_audit when 0 then '未审核' when 2 then '区县审核' when 3 then '市级审核' when 4 then '省级审核' when 5 then '审核完成' when -1 then '退回' END) AS pro_audit,(CASE user_surveyor_type when 2 then '县属' when 3 then '市属' when 4 then '省属' END) AS user_surveyor_type FROM " . $db->table('project') . " WHERE 1";
$sql .= " ORDER BY pro_id DESC";
$row = $db->queryall($sql);
//读取用户信息;
foreach ($row as $k => $v) {
$sql_u = "select user_phone from " . $db->table('user');
$sql_u .= " WHERE user_id =" . $v['user_id'];
$row[$k]["user"] = $db->queryall($sql_u);
}
//读取负责人信息;
foreach ($row as $k => $v) {
$sql_l = "select leader_name,leader_phone from " . $db->table('leader');
$sql_l .= " WHERE pro_id =" . $v['pro_id'];
$row[$k]["leader"] = $db->queryall($sql_l);
}
//读取机构信息;
foreach ($row as $k => $v) {
$sql_a = "select agency_name from " . $db->table('agency');
$sql_a .= " WHERE pro_id =" . $v['pro_id'];
$row[$k]["leader"] = $db->queryall($sql_a);
}
$infoName = "项目信息表" . date("Y.m.d");
@lockdata.cn
边栏推荐
- 三分建设,七分管理!产品、系统、组织三管齐下节能降耗
- SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
- Mockito unit testing
- 机器学习模型的“可解释性”
- 自定义通用分页标签02
- QNX Hypervisor] 10.2 vdev 8259 2.2 user manual
- docker+网桥+redis主从+哨兵模式
- 如何读取 resources 目录下的文件路径?
- 【Playwright测试教程】5分钟上手
- [Study Notes Dish Dog Learning C] Dynamic Memory Management
猜你喜欢

目标检测-中篇

sql语句查询String类型字段小于10的怎么查

一文看懂推荐系统:召回05:矩阵补充、最近邻查找,工业界基本不用了,但是有助于理解双塔模型

从图文展示到以云为核,第五代验证码独有的策略情报能力

【项目实现】Boost搜索引擎
The general SQL injection flow (sample attached)

【医保科普】维护医保基金安全,我们可以这样做

new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail

JVM内存和垃圾回收-07.堆

Homemade bluetooth mobile app to control stm8/stm32/C51 onboard LED
随机推荐
一文看懂推荐系统:召回04:离散特征处理,one-hot编码和embedding特征嵌入
In a more general sense, calculating the displacement distance and assumptions
怎样提高网络数据安全性
Basic form validation process
马尔可夫链
【源码】使用深度学习训练一个游戏
基地址:环境变量
Deep Learning (3) Classification Theory Part
基于Qt的目录统计QDirStat
MCU C language -> usage, and meaning
keytool命令
十一种概率分布
DIY电工维修如何拆卸和安装开关面板插座
Homemade bluetooth mobile app to control stm8/stm32/C51 onboard LED
Shell 函数
学会iframe并用其解决跨域问题
内网服务器访问远程服务器的端口映射
机器学习模型的“可解释性”
数组相关 内容 解析
LeetCode每日一题(2285. Maximum Total Importance of Roads)