SQL query in codeigniter version conflict - php

I have the Following SQL Query whic runs perfectly in codeigniter version 3
It Shows error in version 2.
$this->db->select('*');
if($cond!=''){
$this->db->where($cond);
}
$this->db->from('(select * from products1 where pr_id in('.implode(",",$condin).') order by pr_id asc) as a ');
$this->db->join(PRICE_TABLE.' as b','a.pr_id=b.pr_id','inner');
$this->db->limit($limit);
$query=$this->db->get();
return $query;
The error is
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order by pr_id asc) as a) INNER JOIN `pr_product_attr_usa_new` as b ON `a`.`pr_i' at line 2
SELECT * FROM ((select * from products1 where pr_id in(752, `6263`, `542`, `2059)` order by pr_id asc) as a) INNER JOIN `pr_product_attr_usa_new` as b ON `a`.`pr_id`=`b`.`pr_id` WHERE `b`.`pr_stock` = 'yes' LIMIT 6
Filename: D:\XAMPP\htdocs\pr_sites\us\chk\system\database\DB_driver.php
Line Number: 331

The issue is related to the code:
in('.implode(",",$condin).')
which output:
(752, `6263`, `542`, `2059)`
instead of:
(752, `6263`, `542`, `2059`)
The ` symbol is outside the ')'.

Related

Convert Mysql statement into Laravel eloquent

Since mysql doesn't allow symbol or parameter in views, I want to put my sql statement direct in my controller. But I keep getting an error.
MySQL statement:
SELECT `nokp`, `tarikh_hadir`, #kod_bah := IF(`kod_bah` IS NULL, #kod_bah,`kod_bah`) 'kod_bah'
FROM (SELECT * FROM v_04_harifullkerjawarga_01 ORDER BY `tarikh_hadir`) t1,(SELECT #kod_bah :=0) t2
where nokp = 8201;
I try to convert in laravel as below:
$results = DB::connection('cams')
->table('v_04_harifullkerjawarga_01')
->select(DB::raw("SELECT `nokp`, `tarikh_hadir`, #kod_bah := IF(`kod_bah` IS NULL, #kod_bah,`kod_bah`) 'kod_bah'"))
->select(DB::raw("SELECT * FROM v_04_harifullkerjawarga_01 ORDER BY `tarikh_hadir`) t1"),
DB::raw("(SELECT #kod_bah :=0) t2"))
->where('nokp','=',8201
->get();
But, I get an error. Please someone can help me solve this.
My error:
SQLSTATE[42000]: Syntax error or access violation: 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 'SELECT * FROM v_04_harifullkerjawarga_01 ORDER BY `tarikh_hadir`) t1, (SELECT #k' at line 1 (SQL: select SELECT * FROM v_04_harifullkerjawarga_01 ORDER BY `tarikh_hadir`) t1, (SELECT #kod_bah :=0) t2 from `v_04_harifullkerjawarga_01` where `nokp` = 8201)

SQL query error in codeigniter

I tried the code below in phpmyadmin, but received a syntax error:
select * from `reviews_az`
left join `restaurants_az` on `reviews_az`.`restaurant_id` = `restaurants_az`.`id`
where `source` LIKE %YELP% order by `reviews_az`.`id` desc limit 6);
This is the error log:
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 '%YELP% order by `reviews_az`.`id` desc limit 6)' at line 1
You are missing single quote in like.
Correct query:
select * from reviews_az left join restaurants_az on reviews_az.restaurant_id = restaurants_az.id where source LIKE '%YELP%' order by reviews_az.id desc limit 6
Use below query. Missing quotes in LIKE.
Change
LIKE '%YELP%'
Query
select * from `reviews_az`
left join `restaurants_az` on `reviews_az`.`restaurant_id` = `restaurants_az`.`id`
where `source` LIKE '%YELP%' order by `reviews_az`.`id` desc limit 6;

Converting Sql query to CodeIgniter Active Records

I wanted to query only those items that have a highest likes.
This is the sql query I want to convert into CodeIgnitor's Active Records:
SELECT *, SUM(like) as totalLikes
FROM tbl_like
GROUP BY uploadID
ORDER BY totalLikes DESC
LIMIT 2
CodeIgniter:
public function get_cheezyPic(){
$this->db->select('uploadID, SUM(like) as totalLikes');
$this->db->from('tbl_like');
$this->db->group_by('uploadID');
$this->db->order_by('totalLikes DESC');
$this->db->limit(2);
$query= $this->db->get();
return $query->result_array();}
But when I try to run this code, I've got this error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like) as totalLikes FROM (tbl_like) GROUP BY uploadID ORDER BY totalLikes ' at line 1
SELECT `uploadID`, SUM(like) as totalLikes FROM (`tbl_like`) GROUP BY `uploadID` ORDER BY `totalLikes` desc LIMIT 2
What's wrong with this code?
Thanks for the help.
This'll work for you. Please note that you were using reserved keyword so it should be enclosed with backtic ``.
public function get_cheezyPic(){
$this->db->select('uploadID, SUM(`like`) as totalLikes',false);
$this->db->from('tbl_like');
$this->db->group_by('uploadID');
$this->db->order_by('totalLikes DESC');
$this->db->limit(2);
$query= $this->db->get();
return $query->result_array();
}

Get Average value of from one table for a specific row in another table php

I am trying to do following.
Select a Hospital with id = hid from table named as hospital.
Add a value "overall_rating" to it and get all the ratings and make avg of it from another table named as hrating
here is my query
$statement = $conn->prepare('SELECT hospital.*,(SELECT AVG(hrating.rating_h) FROM hrating WHERE hid = hospital.hid) as overall_rating WHERE hid=:hid LIMIT 1');
Getting this error
{"error":"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE hid='44' LIMIT 1' at line 1"}
Where am i being wrong.
It appears you don't have a " FROM hospital " bit in your query?
SELECT hospital.*,(SELECT AVG(hrating.rating_h)
FROM hrating
WHERE hid = hospital.hid) as overall_rating
FROM hospital -- this line seems to be missing ??
WHERE hid=:hid LIMIT 1
Try this:
SELECT hospital.*, temp.overall_rating
FROM hospital
LEFT JOIN (SELECT hid AVG(rating_h) as overall_rating
FROM hrating group by hid
) temp
on hid = hospital.hid
WHERE hospital.hid=:hid LIMIT 1

MySQL error Reatest-n-per-group

Help me!
SELECT ma_forum.*, ma_forum_cat.*
FROM ma_forum, ma_forum_cat
JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
GROUP BY ma_forum.not_id
WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."'
AND ma_forum.not_status='Ativo'
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='1' ' at line 9"
Your query is in the wrong order; GROUP BY should be after WHERE and before ORDER BY:
SELECT ma_forum.*, ma_forum_cat.*
FROM ma_forum, ma_forum_cat
JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."'
AND ma_forum.not_status='Ativo'
GROUP BY ma_forum.not_id
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC
I don't know what database you're using, but here's a link to MySQL's SELECT syntax

Categories