我如何在 codeigniter 模型中编写简单的 mysql 查询
Posted
技术标签:
【中文标题】我如何在 codeigniter 模型中编写简单的 mysql 查询【英文标题】:How i write simple mysql query in codeigniter model 【发布时间】:2021-08-18 19:45:44 【问题描述】:SELECT DISTINCT
SUBSTRING_INDEX(SUBSTRING_INDEX(process_disposition.sub_dispo, ',', numbers.n), ',', -1) name
FROM
(SELECT 1 n UNION ALL SELECT 2
UNION ALL SELECT 3 UNION ALL SELECT 4) numbers INNER JOIN process_disposition
ON CHAR_LENGTH(process_disposition.sub_dispo)
-CHAR_LENGTH(REPLACE(process_disposition.sub_dispo, ',', ''))>=numbers.n-1
【问题讨论】:
我想在 Codeigniter 3 框架的模型中写这个 【参考方案1】:你可以这样写
$query = $this->db->query("
SELECT DISTINCT SUBSTRING_INDEX
(SUBSTRING_INDEX(process_disposition.sub_dispo, ',', numbers.n), ',', -1)name FROM
(SELECT 1 n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4)numbers
INNER JOIN process_disposition ON CHAR_LENGTH(process_disposition.sub_dispo)
-CHAR_LENGTH(REPLACE(process_disposition.sub_dispo, ',', ''))>=numbers.n-1"
);
if ($query->num_rows() > 0)
return $query->result();
else
return false;
【讨论】:
【参考方案2】:应该这样做。
$this->db->select("DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(process_disposition.sub_dispo, ',', numbers.n), ',', -1) name");
$this->db->from("(SELECT 1 n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4) numbers")
$this->db->join("process_disposition", "CHAR_LENGTH(process_disposition.sub_dispo)-CHAR_LENGTH(REPLACE(process_disposition.sub_dispo, ',', ''))>=numbers.n-1", "INNER");
$result = $this->db->get();
【讨论】:
当我使用这个不需要的空间时,它会自动在查询中查看 echo $this->db->last_query();这给了我 SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(process_disposition.sub_dispo, ', ', numbers.n), ', ', -1) sub_dispo FROM (SELECT 1 n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4) numbers INNER JOINprocess_disposition
ON CHAR_LENGTH(process_disposition.sub_dispo)-CHAR_LENGTH(REPLACE(process_disposition.sub_dispo, ',', ''))>=numbers.n-1以上是关于我如何在 codeigniter 模型中编写简单的 mysql 查询的主要内容,如果未能解决你的问题,请参考以下文章