MYSQL update clause comparing the WHERE from 2 tables - mysql

$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";

Related

SQL syntax error - multiple values from two tables & insert total

I need some help with the query below - I am trying to pull information regarding price and multiply with the quantity & insert the sum into the table. So far I have,
update passenger_baggage
SET passenger_baggage.total_baggage_cost=passenger_baggage.passenger_baggage_quantity*baggage_type.baggage_type_cost
FROM passenger_baggage INNER JOIN baggage_type
ON passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id
WHERE passenger_id = "3";
and getting this 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 passenger_baggage INNER JOIN baggage_type ON
passenger_baggage.passenge...' at line 3
Expecting the query to multiply the two values & insert the total.
There is no FROM clause in an UPDATE query.
Try with this modified query:
update passenger_baggage
INNER JOIN baggage_type
ON passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id
SET passenger_baggage.total_baggage_cost = passenger_baggage.passenger_baggage_quantity * baggage_type.baggage_type_cost
WHERE passenger_id = "3";
Try this:
UPDATE passenger_baggage, baggage_type
SET passenger_baggage.total_baggage_cost = passenger_baggage.passenger_baggage_quantity * baggage_type.baggage_type_cost
WHERE passenger_baggage.passenger_baggage_id = baggage_type.baggage_type_id AND passenger_id = "3";
Saw an example from the MySQL doc (https://dev.mysql.com/doc/refman/8.0/en/update.html)

MySQL: Updating data from one column to another using two matching columns

I'm trying to update columns based on two matching columns: firms.id = investors.firm and investors.firm_role = "ceo", but the server keeps rejecting my syntax. I have tried inner joins, without inner joins, it's all really too complex for me, so I'm not sure what to do next.
UPDATE firms
JOIN investors USING (firms.id = investors.firm)
SET firms.ceo = investors.name
WHERE investors.firm_role = "ceo"
Error:
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 '.id = investors.firm) SET firms.ceo = investors.name WHERE investors.firm_role =' at line 1
You are very close with your syntax:
UPDATE firms
LEFT JOIN investors ON firms.id = investors.firm
SET firms.ceo = investors.name
WHERE investors.firm_role = "ceo"
Refer to this.
Try this Syntax
update patients set column_name="value" where (condition)
for example im using c# and it goes
update patients set " + dgv.Columns[c].HeaderText + "='" + dgv.Rows[dgv.CurrentCell.RowIndex].Cells[c].Value.ToString() + "' where idno='" + dgv.Rows[dgv.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'"
You may try this
UPDATE firms
INNER JOIN investors ON firms.id = investors.firm AND investors.firm_role = "ceo"
SET firms.ceo = investors.name

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.

Is it possible to insert value in table based on parameter value

String sql = ("insert into registration(pic) values(?) where email='"+Email+"' ");
i get error :error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where email='yyy#ymail.com'' at line 1
You have to use UPDATE query to pass it like
String sql = "UPDATE registration SET pic = ? WHERE email = '" + Email + "'";
Syntax for UPDATE query is
UPDATE table_name SET column_name = value;
Insert query format should be,
"insert into tablename (columnname) values(coulmnvalue)"
OR
"update registration set pic='' where email='"+Email+"'";
Yes. that is impossible.
Either you want:
insert into registration(pic) values(?)
Which will give you a new row;
Or you want an UPDATE:
UPDATE registration SET pic = ?
WHERE email = <EMAILYouWant>
Which will update an existing row where email = the record with the email you want to update the pic column.