codeigniter2多连接和where语句

pdtvr36n  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(281)

更新的代码

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

1aaf6o9v

1aaf6o9v1#

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

cgh8pdjw2#

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

相关问题