thinkphp:判断数据是否存在,再作为数据库的判断条件(给数据库查询增加额外的查询条件)

方法一:用thinkphp的语法去写

php 复制代码
public function select_endProduceinfo(){
    $like_info = input('post.like_info', '');
    $page = input('post.page', 1);
    $pageSize = input('post.pageSize', 10);
    $start = ($page - 1) * $pageSize;
    $username = input('post.username','');
    $search_line1 = input('post.search_line1','');
    $search_line2 = input('post.search_line2','');
    $search_line3 = input('post.search_line3','');
    $search_line4 = input('post.search_line4','');
    $employee_num = db::table('fa_account_info')->where(['username' => $username])->value('employee_num');
  //全部数据信息  
    $data['info'] = db::table('wip_transactions')
                    ->alias('d') // 设置wip_jobs_all的别名
                    ->join(['wip_jobs_all' => 'a'], 'd.wip_entity_name = a.wip_entity_name')
                    ->join(['sf_item_no' => 'c'], 'a.primary_item = c.item_no')
                    ->join(['hr_employees' => 'e'], 'e.employee_num = d.employee_num')
                    ->field('d.*, c.item_no as item_no, c.item_name as item_name, c.units as uom, e.employee_name as employee_name')
                    ->where(['d.employee_num' => $employee_num])
                    ->where(['d.wip_entity_name'=>['like','%'.$like_info.'%']]);
    // 添加查询条件
    if (!empty($search_line1)) {
        $data['info']->where('d.wip_entity_name', 'LIKE', "%$search_line1%");
    }
    if (!empty($search_line2)) {
        $data['info']->where('d.operation_code', 'LIKE', "%$search_line2%");
    }
     if (!empty($search_line3)) {
        $data['info']->where('item_no', 'LIKE', "%$search_line3%");
    }
     if (!empty($search_line4)) {
        $data['info']->where('item_name', 'LIKE', "%$search_line4%");
    }
    $data['info'] = $data['info']
        ->order(['transaction_date' => 'desc'])
        ->limit($start, $pageSize)
        ->select();
//求出数据总数
    $data['total'] =  db::table('wip_transactions')
                    ->alias('d') // 设置wip_jobs_all的别名
                    ->join(['wip_jobs_all' => 'a'], 'd.wip_entity_name = a.wip_entity_name')
                    ->join(['sf_item_no' => 'c'], 'a.primary_item = c.item_no')
                    ->join(['hr_employees' => 'e'], 'e.employee_num = d.employee_num')
                    ->field('d.*, c.item_no as item_no, c.item_name as item_name, c.units as uom, e.employee_name as employee_name')
                    ->where(['d.employee_num' => $employee_num])
                    ->where(['d.wip_entity_name'=>['like','%'.$like_info.'%']]);  
    // 添加查询条件
     if (!empty($search_line1)) {
        $data['info']->where('d.wip_entity_name', 'LIKE', "%$search_line1%");
    }
    if (!empty($search_line2)) {
        $data['info']->where('d.operation_code', 'LIKE', "%$search_line2%");
    }
     if (!empty($search_line3)) {
        $data['info']->where('item_no', 'LIKE', "%$search_line3%");
    }
     if (!empty($search_line4)) {
        $data['info']->where('item_name', 'LIKE', "%$search_line4%");
    }         
    $data['total'] = $data['total']
        ->order(['transaction_date' => 'desc'])
        ->count();
    //处理数据
    for ($i = 0; $i < count($data['info']); $i++) {
      //计算时差
      $data['info'][$i]['hours_diff'] = number_format(($data['info'][$i]['end_date'] - $data['info'][$i]['begin_date']) / 3600, 4);
      if ($data['info'][$i]['transaction_type'] != '良品') {
        $data['info'][$i]['hours_diff'] = '';
      }
      //处理时间
      $data['info'][$i]['transaction_date'] = date('Y-m-d', $data['info'][$i]['transaction_date']);
      $data['info'][$i]['begin_date'] = date('Y-m-d H:i:s', $data['info'][$i]['begin_date']);
      $data['info'][$i]['end_date'] = date('Y-m-d H:i:s', $data['info'][$i]['end_date']);
      if (!$data['info'][$i]['transaction_quantity']) {
        $data['info'][$i]['transaction_quantity'] = 0;
      }
    }
    echo json_encode($data);
}

方法二:嵌套mysql的语法

php 复制代码
public function select_POorder_deatil(){
    $page = input('post.page', 1);
    $pageSize = input('post.pageSize', 10);
    $start = ($page - 1) * $pageSize;
    $order_number = input('post.order_number','');
    //头信息
    $header_info = "
            SELECT  pha.po_num, pha.status,order_type, pha.note,pha.payment_term ,pha.order_date,pha.app_remark, pha.creation_date, pha.tax_amount,
                    pha.tax_flag,pha.all_line_amount, pha.created_by, pha.youhui_amount, vendor_name,pha.po_all_amount,pha.vendor_code,
                    pha.payment_type,pha.tax_name 
            FROM    po_headers_all pha, vendors v
            WHERE   v.vendor_code = pha.vendor_code
            AND     po_num= '" .$order_number."'
        ";
    $data['header_info'] =  Db::query($header_info);
    //处理日期
    for($i=0;$i<count($data['header_info']);$i++){
        $data['header_info'][$i]['creation_date'] = date('Y-m-d H:i:s', $data['header_info'][$i]['creation_date']);
        $data['header_info'][$i]['order_date'] = date('Y-m-d', $data['header_info'][$i]['order_date']);
    }
    //行信息
    $line_info = "
            SELECT  a.line,a.stockid,b.item_desc,b.item_name,a.uom,a.price,a.quantity,a.po_num,a.line_amount,a.last_update_date,
                    a.creation_date,ifnull(quantity_received,0) quantity_received,ifnull(quantity_accepted,0) quantity_accepted,
                    ifnull(quantity_deliveried,0) quantity_deliveried,ifnull(quantity_cancelled,0) quantity_cancelled,
                    ifnull(quantity_rejected,0) quantity_rejected,ifnull(quantity_billed,0) quantity_billed,a.created_by,
                    (select locationname from locations d where d.loccode=a.subinventory_code) locationname,a.need_date
            FROM    po_lines_all a,sf_item_no b
            where   a.stockid=b.item_no   
            and     po_num = '" .$order_number."' 
            ";
    $line_info .="  
                    ORDER BY a.line ASC
                    LIMIT $start, $pageSize 
                ";    
    $data['line_info'] =  Db::query($line_info);
     //处理日期
    for($i=0;$i<count($data['line_info']);$i++){
        $data['line_info'][$i]['creation_date'] = date('Y-m-d H:i:s', $data['line_info'][$i]['creation_date']);
    }
    //行总数
    $line_count = "
            SELECT COUNT(*) AS count
            FROM (
                SELECT  a.line,a.stockid,b.item_desc,b.item_name,a.uom,a.price,a.quantity,a.po_num,a.line_amount,a.last_update_date,
                    a.creation_date,ifnull(quantity_received,0) quantity_received,ifnull(quantity_accepted,0) quantity_accepted,
                    ifnull(quantity_deliveried,0) quantity_deliveried,ifnull(quantity_cancelled,0) quantity_cancelled,
                    ifnull(quantity_rejected,0) quantity_rejected,ifnull(quantity_billed,0) quantity_billed,a.created_by,
                    (select locationname from locations d where d.loccode=a.subinventory_code) locationname,a.need_date
            FROM    po_lines_all a,sf_item_no b
            where   a.stockid=b.item_no   
            and     po_num = '" .$order_number."' 
            ) AS subquery;";
    $result = Db::query($line_count);
    $data['line_info_total'] = isset($result[0]['count']) ? $result[0]['count'] : 0;    
    echo json_encode($data);
}
相关推荐
时差95331 分钟前
【面试题】Hive 查询:如何查找用户连续三天登录的记录
大数据·数据库·hive·sql·面试·database
让学习成为一种生活方式33 分钟前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
秋意钟1 小时前
MySQL日期类型选择建议
数据库·mysql
Dxy12393102162 小时前
python下载pdf
数据库·python·pdf
桀桀桀桀桀桀2 小时前
数据库中的用户管理和权限管理
数据库·mysql
superman超哥4 小时前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba
用户8007165452004 小时前
HTAP数据库国产化改造技术可行性方案分析
数据库
engchina4 小时前
Neo4j 和 Python 初学者指南:如何使用可选关系匹配优化 Cypher 查询
数据库·python·neo4j
engchina4 小时前
使用 Cypher 查询语言在 Neo4j 中查找最短路径
数据库·neo4j
尘浮生4 小时前
Java项目实战II基于Spring Boot的光影视频平台(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·后端·maven·intellij-idea