简体   繁体   English

Codeigniter 2多重连接和where语句

[英]Codeigniter 2 multiple join and where statement

Updated code 更新的代码

$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();

What I'm try to accomplish, is to display all documents located in our office that are Processing and has action 1 . 我要完成的工作是显示位于我们办公室的所有正在处理并有操作1的文档。 Documents may have tags like memo, request, finance, etc. The output is incorrect and not showing all the records that is in our office. 文档中可能包含诸如备忘录,请求,财务等标签。输出不正确,并且未显示我们办公室中的所有记录。 I think there is a problem in WHERE clause? 我认为WHERE子句有问题吗? What could be the culprit in my code? 我代码中的罪魁祸首是什么?

Update your query by removing the single quotes surrounding 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";
$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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM