【问题标题】:Codeigniter 2 multiple join and where statementCodeigniter 2 多重连接和 where 语句
【发布时间】:2018-05-30 02:23:48
【问题描述】:

更新代码

$office = $this->session->userdata('department');

$query = "SELECT `doc`.`id`, `doc`.`barcode`, `doc`.`sub`, `doc`.`source_type`, `doc`.`sender`, `doc`.`address`, `doc`.`description`, `doc`.`receipient`, `doc`.`status`, DATE_FORMAT(`doc`.`datetime_added`, '%m/%d/%Y-%h:%i %p') as datetime_added,
          (SELECT GROUP_CONCAT(`tag`) FROM `tags` WHERE `tags`.`documentId` = `doc`.`id` GROUP BY `tags`.`documentId`) as `tags`
          FROM `documents` AS `doc`
          JOIN `transactions` AS `trans` ON `doc`.`id` = `trans`.`document_id`
          JOIN `trackers` AS `track` ON `doc`.`id` = `track`.`document_id`
          WHERE `doc`.`status` = 'Processing'
          AND `track`.`action` = '1')
          AND `track`.`location` = '$office'
          ORDER BY `doc`.`id` DESC";
$go = $this->db->query($query)->result_array();
var_dump($go); exit();

我试图完成的是显示位于我们办公室的所有文档,这些文档正在处理并且具有操作1。文件可能有备忘录、请求、财务等标签。输出不正确,未显示我们办公室的所有记录。我认为 WHERE 子句有问题?我的代码中的罪魁祸首可能是什么?

【问题讨论】:

  • 您将需要加入一个子查询,该子查询通过idtags 表上执行GROUP_CONCAT,以生成每个文档的所有标签的列表。我在您的代码中看到太多问题,无法尝试正式回答。
  • 嗨@TimBiegeleisen 我目前正在做一个自定义查询。稍后我会更新代码。你看到了什么问题,所以我可以解决它?非常感谢!

标签: php mysql sql join codeigniter-2


【解决方案1】:

通过删除围绕 1 的单引号来更新您的查询:

$query = "SELECT `doc`.`id`, `doc`.`barcode`, `doc`.`sub`, `doc`.`source_type`, `doc`.`sender`, `doc`.`address`, `doc`.`description`, `doc`.`receipient`, `doc`.`status`, DATE_FORMAT(`doc`.`datetime_added`, '%m/%d/%Y-%h:%i %p') as datetime_added,
          (SELECT GROUP_CONCAT(`tag`) FROM `tags` WHERE `tags`.`documentId` = `doc`.`id` GROUP BY `tags`.`documentId`) as `tags`
          FROM `documents` AS `doc`
          JOIN `transactions` AS `trans` ON `doc`.`id` = `trans`.`document_id`
          JOIN `trackers` AS `track` ON `doc`.`id` = `track`.`document_id`
          WHERE `doc`.`status` = 'Processing'
          **AND `track`.`action` = 1)**
          AND `track`.`location` = '$office'
          ORDER BY `doc`.`id` DESC";

【讨论】:

    【解决方案2】:
    $this->db->select("doc.id, doc.barcode, doc.sub, doc.source_type, doc.sender, doc.address, doc.description, doc.receipient, doc.status, DATE_FORMAT(doc.datetime_added, %m/%d/%Y-%h:%i %p) as datetime_added,
          (SELECT GROUP_CONCAT(tag) FROM tags WHERE tags.documentId = doc.id GROUP BY tags.documentId) as tags")
          ->from("documents AS doc") 
          ->join("transactions AS trans",'doc.id=trans.document_id')
          ->join("trackers AS track",'doc.id=track.document_id')
          ->where("doc.status","Processing")
          ->andWhere("track.action","1")
          ->andWhere("doc.id",$office)
          ->order_by("doc.id", "DESC");
    

    【讨论】:

      猜你喜欢
      • 2013-09-26
      • 2015-04-27
      • 2017-03-31
      • 2018-06-15
      • 2011-12-12
      • 2015-12-11
      相关资源
      最近更新 更多