0
votes

I'm trying to convert this mysql query to active record, but the like clause not working properly and not return any output. I don't get what I missed actually.

table schema:

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 Query: (Working perfectly)

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='[email protected]')),'%')

Codeigniter Model:

    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

1 Answers

0
votes

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(); 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` = \'[email protected]\')%' 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` = \'[email protected]\'))%' 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. 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();