简体   繁体   中英

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 . 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? What could be the culprit in my code?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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