SQL query syntax error, UPDATE statement with INNER JOIN - mysql

my question should be very easy to fix but I can't find the syntax error that my db interface throws at me. The following code is used to update the password of an existing user in a timetrackingtool.
I checked all the tables and field names, they all are right.
The SQL query code:
$query = ('UPDATE tblMitarbeiterUUID SET tblMitarbeiterUUID.dtPassword="' . $pwd . '" '.
'INNER JOIN arbeiter '.
'ON tblMitarbeiterUUID.idMitarbeiterUUID=arbeiter.fidMitarbeiterUUID ' .
'WHERE arbeiter.id=' . $userID)
Echo of this query:
UPDATE tblMitarbeiterUUID SET tblMitarbeiterUUID.dtPassword="7687225fde7aad38f4c005ad4b5cdd5a" INNER JOIN arbeiter ON tblMitarbeiterUUID.idMitarbeiterUUID=arbeiter.fidMitarbeiterUUID WHERE arbeiter.id=1
Error I get when trying to run the query directly on the db:
SQL error (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 'FROM tblMitarbeiterUUID INNER JOIN arbeiter ON tblMitarbeiterUUID.ifMitarbeiterUUID=arbeiter.fidMitar...' at line 1
In my research I saw some people using a FROM in a UPDATE statement with an INNER JOIN. I tried it with a FROM but I get the same error as above.
Thanks for your help in advance. Best Regards, Luca.
correct syntax (edited after I got the correct answer):
'UPDATE tblMitarbeiterUUID x '.
'JOIN arbeiter y ON x.idMitarbeiterUUID = y.fidMitarbeiterUUID ' .
'SET x.dtPassword="' . $pwd . '" ' .
'WHERE y.id=' . $userID

This is valid syntax:
UPDATE tblMitarbeiterUUID x
JOIN arbeiter y
ON x.idMitarbeiterUUID = y.fidMitarbeiterUUID
SET x.dtPassword="A"
WHERE y.id=1

Related

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.

SQL Syntax Error in PHP code - adding together two values

I keep getting this syntax error in my MySQL code within a PHP file. I'm simply trying to increment/add to the value already in the table with this time variable. If anyone could help me out I'd greatly appreciate it.
PHP:
$sql = "UPDATE Aircraft
SET MaintenanceFlightTime = (MaintenanceFlightTime + $MaintenanceDuration),
WHERE AircraftID = $AircraftID";
Error:
UPDATE Aircraft SET MaintenanceFlightTime = (MaintenanceFlightTime + 00:10:00), WHERE AircraftID = 8
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 ':10:00), WHERE AircraftID = 8' at line 2
You cannot just add a string like '01:01:01' to a time column but you can use ADDTIME()
$MaintenanceDuration = '00:10:00';
$sql = "UPDATE Aircraft
SET MaintenanceFlightTime = ADDTIME(MaintenanceFlightTime, '$MaintenanceDuration')
WHERE AircraftID = $AircraftID";

Mysql: UPDATE, SET & WHERE error

I have a database named prestashop, a table ps_product and a field mq which is tinyInt.
If I do this query on console, it runs correctly, editing the value of a field in a record:
USE prestashop UPDATE ps_product SET mq = 0 WHERE id_product = 1
If I do this query programmatically (php):
mysql_query("USE prestashop
UPDATE ps_product
SET mq = 0
WHERE id_product = 1;")
or die("Query non valida: " . mysql_error());
and this is the error:
Query non valida: 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 'UPDATE ps_product SET mq = 0 WHERE id_product = 1' at line 2
why?
In the console all proceeds correctly, and it really edit the value on the table
For update product detail in prestashop you should write query like that:
$sql = 'UPDATE '._DB_PREFIX_.'product SET mq=0 WHERE id_product=1';
if (!Db::getInstance()->execute($sql))
die('error!');
I hope it would help you.

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.

MYSQL update clause comparing the WHERE from 2 tables

$produpd = "UPDATE tblnavpc SET tblnavpc.ChildName = tblnav.NavName " .
"FROM tblnav WHERE tblnavpc.CID = tblnav.NavID";
This is the error I get"
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 'FROM tblnav
WHERE tblnavpc.CID = tblnav.NavID' at
line 1
I know keys aren't named greatly but I just trying to fix this one problem, I didn't name the tables.
You don't have a FROM clause in an update:
UPDATE tblnavpc
INNER JOIN tblnav ON tblnavpc.CID = tblnav.NavID
SET tblnavpc.ChildName = tblnav.NavName
"From" cannot be used with update statements.
Should be
$produpd = "UPDATE tblnavpc SET tblnavpc.ChildName = tblnav.NavName " .
"WHERE tblnavpc.CID = tblnav.NavID";