Error Number: 1064 when select from database - mysql

when select images from a database below error is shown & after i delete record in the database its remain the same number inside (id)
this is the error
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 '16,19,1,2,1,2,1 )' at line 1
select * from images where id in(,16,19,1,2,1,2,1 )
Filename: controllers/Admin.php
Line Number: 1046
and this is select statment :
if (!empty($data->images)) {
$qry = $this->db->query("select * from images where id in($data-
>images )");
$res['results'] = $qry->result();
}
and now my database is empty and this error still remaining ?

just remove the leading comma. like this.
select * from images where id in( 16,19,1,2,1,2,1 )
there is a leading comma in $data->images

Related

Trying to perform divison using MySQL subqueries but getting syntax error

I am trying to get the value from MySQL subqueries but I am getting 'Error Code: 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 ')' at line 1'
The code is:
UPDATE national_avg_tariff SET Value= (CASE WHEN ((SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))>0)
THEN
(SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))
ELSE NULL END) where FiscalYear=2020 AND Parameter='Fixed Charge(%)';
The code doesn't show any red underline or pre-execution error something, the error only appears once I execute it.
Any help in rectifying syntax error of this piece of code would be appreciable. Thanks

Yii v1 Left Join giving CDbException

I am trying to do an LEFT Join in Yii v.1 Mysql .
There are alreday 4 tables joined and I want to join this table also.
$cmd = Yii::app()->db->createCommand();
$selectString .= "table2.dir_pic, table2.pic1, table2.pic2 , table2.pic3 ";
$cmd->select($selectString);
$cmd->from('table1');
$cmd->leftjoin('table2', 'table1.id=table2.user_entry_id');
$entries = $cmd->queryAll();
$this->render('index', compact('entries', 'model'));
It is giving an CDbException
CDbCommand failed to execute the SQL statement: 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 '.`dir_pic`, `table2`.`pic1`, `table2`.`pic2`, ' at line 1.
I tried this query in mysql(phpmyadmin)
SELECT table1.id,table2.dir_pic,table2.pic1,table2.pic2, table2.pic3 FROM
table1 LEFT JOIN table2 ON table1.id = table2.user_entry_id
and It shows proper output.BUt not when I try in Yii .
I found the solution. Actually I missed an comma (,) after the end of previous $selectString query.

Why is this a SQL syntax error?

I have this query in Perl:
my $pkg="%";
my $sql = "SELECT pid, CAST(pid as UNSIGNED) AS l FROM xmld ORDER BY l WHERE pkg LIKE ?";
my $files_ref = $dbh->selectcol_arrayref($sql, undef, $pkg);
It crashes with:
DBD::mysql::db selectcol_arrayref failed: 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 pkg LIKE '%'' at line 1 at ...
I have been looking at this statement for hours, tried various things, but no luck.
Where is that extra single quote coming from and how do I get rid of it?
order by goes after the where:
SELECT pid, CAST(pid as UNSIGNED) AS l
FROM xmld
WHERE pkg LIKE ?
ORDER BY l;

SQL syntax error in query

What is wrong with my MySQL Query that is displaying the 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 'index WHERE id=1' at line 1
Relevant code:
$qu = mysql_query("SELECT * FROM index WHERE id= 1") or die("MySQL ERROR: ".mysql_error());
WHILE($d = mysql_fetch_array($qu)):
$con = $d['content'];
endwhile;
index is a MySQL reserved keyword, so you must quote it with backticks like this:
SELECT *
FROM `index`
WHERE id = 1

mysql: Error Number: 1064

I'm trying to delete some data from 3 tables. Here is my SQL:
DELETE
FROM productdetail
JOIN (productdescription,productmineralcategories,productspeciescategories)
ON
(
productdetail.id = productspeciescategories.id_product
AND productdetail.id = productmineralcategories.id_product
AND productdetail.id = productdescription.id_product
)
WHERE productdetail.id='".$data['id'].
And here is the output error:
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 'JOIN (productdescription, productmineralcategories,
productspeciescategories) ' at line 3
What does it mean and how can I fix it?
Your DELETE statement should be:
$query="DELETE
FROM productdetail
WHERE productdetail.id='".$data[$id] . "'";
OR
$query="DELETE
FROM productdetail
WHERE productdetail.id='$data[$id]'";
OR do not add single quote if field type is numeric.
$query="DELETE
FROM productdetail
WHERE productdetail.id=$data[$id]";
Have a look at DELETE JOIN syntax.