简体   繁体   English

codeigniter:活动记录,如子句不起作用

[英]codeigniter: active record like clause not working

I'm trying to convert this mysql query to active record, but the like clause not working properly and not return any output. 我正在尝试将此mysql查询转换为活动记录,但是like子句无法正常工作,并且不返回任何输出。 I don't get what I missed actually. 我没有得到我真正想念的东西。

table schema: 表架构:

tbl_batch: tbl_batch:

CREATE TABLE `tbl_batch` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `Round` varchar(5) COLLATE utf8_bin NOT NULL,
 `BatchID` varchar(255) COLLATE utf8_bin NOT NULL,
 `TSP` varchar(10) COLLATE utf8_bin NOT NULL,
 `Slot` tinyint(1) NOT NULL,
 `Trade` int(11) NOT NULL,
 `StartDate` date NOT NULL,
 `EndDate` date NOT NULL,
 PRIMARY KEY (`BatchID`),
 UNIQUE KEY `ID` (`ID`),
 UNIQUE KEY `BatchID` (`BatchID`),
 UNIQUE KEY `BatchID_2` (`BatchID`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

tbl_tsp_info: tbl_tsp_info:

CREATE TABLE `tbl_tsp_info` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `TSP` varchar(10) NOT NULL,
 `Name` varchar(50) NOT NULL,
 `Email` varchar(50) NOT NULL,
 `Website` varchar(50) NOT NULL,
 `ContactPerson` varchar(50) NOT NULL,
 `ContactPhone` varchar(11) NOT NULL,
 `Phone` varchar(11) NOT NULL,
 `Fax` varchar(11) NOT NULL,
 `Address` varchar(255) NOT NULL,
 PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4

tbl_instructor_info tbl_instructor_info

CREATE TABLE `tbl_instructor_info` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `InstructorID` varchar(7) NOT NULL,
 `Name` varchar(50) NOT NULL,
 `Mobile` varchar(11) NOT NULL,
 `Email` varchar(50) NOT NULL,
 `Trade` int(1) NOT NULL,
 `TSP` int(1) NOT NULL,
 `Slot` tinyint(1) NOT NULL,
 PRIMARY KEY (`ID`),
 UNIQUE KEY `InstructorID` (`InstructorID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8

MySQL Query: (Working perfectly) MySQL查询:(完美运行)

SELECT BatchID 
from tbl_batch
WHERE Round='7' 
AND BatchID LIKE concat('%', (SELECT ShortCode FROM tbl_tsp_info WHERE id=
    (SELECT TSP FROM tbl_instructor_info WHERE Email='monir.ssts@gmail.com')),'%')

Codeigniter Model: Codeigniter模型:

    public function get_batch_byUser($email=string) {
        $this->db->select('TSP');
        $this->db->from('tbl_instructor_info');
        $this->db->where('email',$email);
        $tsp = $this->db->get_compiled_select();

        $this->db->select('ShortCode');
        $this->db->from('tbl_tsp_info');
        $this->db->where("`id`= ($tsp)", null, false);
        $batchCode = $this->db->get_compiled_select();

        $this->db->select('BatchID ');
        $this->db->from('tbl_batch');
        $this->db->where('round',7);
        $this->db->like('BatchID', $batchCode);
        $query = $this->db->get();

        return $query->result();
    }

I see the problem but the solution may or may not work. 我看到了问题,但解决方案可能会或可能不会起作用。

Here's how to see the problem. 这是解决问题的方法。 Replace the last line of code your code return $query->result(); 替换代码的最后一行,代码return $query->result(); with

echo $this->db->get_compiled_select();

You will see 你会看见

SELECT `BatchID` 
FROM `tbl_batch` 
WHERE `round` = 7 
AND `BatchID` LIKE '%SELECT `ShortCode`\nFROM `tbl!_tsp!_info`
\nWHERE `id`= (SELECT `TSP`\nFROM `tbl!_instructor!_info`
\nWHERE `email` = \'me@example.com\')%' ESCAPE '!'

See where the wildcards characters (%) are? 看到通配符(%)在哪里? Not going to work. 不上班

If you change 如果你改变

$this->db->like('BatchID', $batchCode);

to

$this->db->like('BatchID', "($batchCode)");

The echo is this 回声是这个

SELECT `BatchID` FROM `tbl_batch` 
WHERE `round` = 7 
AND `BatchID` 
LIKE '%(SELECT `ShortCode`\nFROM `tbl!_tsp!_info`
\nWHERE `id`= (SELECT `TSP`\nFROM `tbl!_instructor!_info`
\nWHERE `email` = \'me@example.com\'))%' ESCAPE '!'

I don't know if that will execute correctly - I don't have the datasets. 我不知道它是否可以正确执行-我没有数据集。 But the syntax looks right. 但是语法看起来正确。 Right? 对?

Sometime "Active Record" (in Codeigniter > v3.0 "Query Builder") is not the most efficient way to get what you need. 有时“ Active Record”(在Codeigniter> v3.0“查询生成器”中)不是获得所需内容的最有效方法。 Falling back to more basic methods will often be a lot less trouble. 退回到更基本的方法通常会减少很多麻烦。

$sql = "SELECT BatchID from tbl_batch WHERE Round='7' AND BatchID 
LIKE concat('%', (SELECT ShortCode FROM tbl_tsp_info 
WHERE id = (SELECT TSP FROM tbl_instructor_info 
    WHERE Email= ?)),'%')";

$query = $this->db->query($sql, [$email]);
return $query->result();

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

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