Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting extra quote in query using codeigniter

I am trying to select and join to other table but my query having extra quote unintentionally.

I use this code:

$this->db->select('TA.*, TC.username user FROM tbl_actlog AS TA');
$this->db->join('(SELECT id, username FROM tbl_login) AS TC', 'TC.id = TA.account_id', 'LEFT OUTER');
$this->db->where('TA.date_log >=', $start_date);
$this->db->where('TA.date_log <=', $end_date);

But when my code runs it gives me an error and the output is:

SELECT `TA`.*, `TC`.`username user FROM tbl_actlog` AS `TA` 
LEFT OUTER JOIN (SELECT id, username FROM tbl_login) AS TC 
ON `TC`.`id` = `TA`.`account_id` 
WHERE `TA`.`date_log` >= '2019-12-10' AND `TA`.`date_log` <= '2019-12-10'
ORDER BY `TA`.`act_id` DESC LIMIT 10

MySQL said: Documentation

#1064 - You have an error in your SQL syntax;  
check the manual that corresponds to your MariaDB server version for the
right syntax to use near 'LEFT OUTER JOIN (SELECT id, username FROM
tbl_login) AS TC ON `TC`.`id` = `TA`.`' at line 1

1 Answers

From what I understand the right query syntax should be:

$this->db->select('TA.*, TC.id, TC.username');
$this->db->from('tbl_actlog AS TA');
$this->db->join('tbl_login AS TC', 'TC.id = TA.account_id', 'LEFT OUTER');
$this->db->where('TA.date_log >=', $start_date);
$this->db->where('TA.date_log <=', $end_date);
like image 82
prashant Avatar answered Jul 01 '26 13:07

prashant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!