首页 文章

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

提问于
浏览
0

我正在尝试将此mysql查询转换为活动记录,但like子句不能正常工作而不返回任何输出 . 我没有得到我实际错过的东西 .

表模式:

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:

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

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查询:(完美运行)

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模型:

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();
    }

1 回答

  • 0

    我看到问题,但解决方案可能会或可能不会 .

    这是如何看待问题 . 用代码 return $query->result(); 替换最后一行代码

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

    你会看见

    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 '!'
    

    查看通配符(%)的位置?不会工作 .

    如果你改变了

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

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

    回声是这样的

    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 '!'
    

    我不知道这是否会正确执行 - 我没有数据集 . 但语法看起来正确 . 对?

    有时“Active Record”(在Codeigniter> v3.0“查询生成器”中)并不是获得所需内容的最有效方式 . 回归到更基本的方法往往会减少麻烦 .

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

相关问题