当前位置:网站首页>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
边栏推荐
- Power button (LeetCode) 215. The first K largest elements in the array (2022.08.03)
- Rongyun "Audio and Video Architecture Practice" technical session [complete PPT included]
- sqoop ETL工具
- 从图文展示到以云为核,第五代验证码独有的策略情报能力
- JVM内存和垃圾回收-07.堆
- MySQL Query Exercise (1)
- 跨境电商看不到另一面:商家刷单、平台封号、黑灰产牟利
- 2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
- 4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
- FFmpeg —— 通过修改yuv,将视频转为黑白并输出(附源码)
猜你喜欢
随机推荐
数据集类型转换—TFRecords文件
千兆2光8电管理型工业以太网交换机WEB管理X-Ring一键环网交换机
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
学会iframe并用其解决跨域问题
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
数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比
全网没有之一的JMeter 接口测试流程详解
Countdown to 2 days, the "New Infrastructure of Cultural Digital Strategy and Ecological Construction of Cultural Art Chain" will kick off soon
汇编语言之栈
Architecture of the actual combat camp module three operations
多线程间的通信方式你知道几种?
自定义通用分页标签02
tkmapper的crud示例:
外卖店优先级
JVM内存和垃圾回收-07.堆
4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
单片机C语言->的用法,和意思
The general SQL injection flow (sample attached)
DIY电工维修如何拆卸和安装开关面板插座
如何读取 resources 目录下的文件路径?