Codeigniter 2多个连接和where语句

时间:2018-05-30 02:23:49

标签: php mysql sql join codeigniter-2

更新了代码

$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 子句存在问题?什么可能是我的代码的罪魁祸首?

2 个答案:

答案 0 :(得分:0)

删除围绕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";

答案 1 :(得分:0)

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