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

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

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)

#Error in SQL while updating table using join

I got an error while updating the table with the join query. The error message is:
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 'from employee details join employeedept on employee details.emp_dept=employeedept.' at line 3
Can anyone help me to resolve this error?
update employeeDetails
set employeeDetails.emp_age = employeeDetails.emp_age+20, employeeDetails.emp_salary = employeeDetails.emp_salary-500
from employeeDetails
join employeedept on employeeDetails.emp_dept = employeedept.dept_name
where employeedept.dept_name = 'CSE';
select * from employeeDetails;
List item
Have you tried using MySQL's UPDATE JOIN syntax yet? e.g.:
update employeeDetails
join employeeDept on employeeDetails.emp_dept = employeeDept.dept_name
set
employeeDetails.emp_age = employeeDetails.emp_age+20,
employeeDetails.emp_salary = employeeDetails.emp_salary-500
where employeeDept.dept_name = 'CSE';
select * from employeeDetails;

UPDATE Query Giving me an SQL Error (1064)

I am trying to update a table in my database from another table. Here is my Syntax, but I can't seem to find any issues with it. I keep getting SQL Error (1064).
UPDATE customers b
SET customers.takerid = customer_update_2016.ot
FROM customer_update_2016 a, customers b
WHERE a.phone = b.phone && a.lname = b.lname
SQL Error (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 'FROM customer_update_2016 a, customers b WHERE a.phone = b.phone & a.lname =b' at line 3
Solution:
UPDATE customers
INNER JOIN customer_update_2016
ON customers.phone = customer_update_2016.phone
AND customers.lname = customer_update_2016.lname
SET customers.takerid = customer_update_2016.ot
you have both mysql and sql-server
which one?
UPDATE customers
SET customers.takerid = customer_update_2016.ot
FROM customers
JOIN customer_update_2016
on customers.phone = customer_update_2016.phone
and customers.lname = customer_update_2016.lname
and customers.takerid <> customer_update_2016.ot
Follow something like this always
UPDATE [table1_name] AS t1
INNER JOIN [table2_name] AS t2
ON t1.[column1_name] = t2.[column1_name]
SET t1.[column2_name] = t2.[column2_name];
To solve your problem you should use JOIN, in my case I had to pass data from one table to another like you and this was the way that solved my problem, hope it help with yours and others.(I'm using MariaDB v10.4)
UPDATE
table1
LEFT JOIN table2 AS tb2
ON tb2.fieldThatJoin = table1.fieldThatJoin//this part indicate the field that join them
SET
table1.field1 = tb2.field1;
//if you want to update more than one field then do this
table1.field1 = tb2.field1,
table1.field2 = tb2.field2,
table1.field3 = tb2.field3;//remember to put the ';' at the end

Yii2: can't create sql command using innerJoin

I am not that good at SQL but i do as much as i can for the little knowledge i have..
I have made a single a flat SQL string with the help from a friend that gathers data from a table using a relative table from an initial data from the first table, the SQL was made like this:
SELECT id, username, auth_assignment.created_at
FROM `user` JOIN `auth_assignment`
ON (user.id = auth_assignment.user_id)
JOIN `auth_item`
ON (auth_item.name = auth_assignment.item_name)
WHERE auth_item.name = 'Admin'
the initial data to look is Admin so everything works in that side, but i tried to simulate this SQL using Yii2 functions.. so far i have made this code
$query = new Query;
$command = $query->select('id, username')
->innerJoin('auth_assignment', ['user.id'=>'auth_assignment.user_id'])
->innerJoin('auth_item', ['auth_item.name'=>'auth_assignment.item_name'])
->where('auth_item.name = :name', [':name'=>$name])->createCommand();
var_dump($command->query());
this returns an SQLSTATE 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 'INNER JOIN `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN' at line 1
The SQL being executed was: SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN `auth_item` ON `auth_item`.`name`='auth_assignment.item_name' WHERE auth_item.name = 'Admin'
i checked the method $command->sql; to know how the SQL was being generated.. but i really don't know how to fix it due to my lack of my knowledge to SQL and lack of understanding to yii2 api documentation
SQL is generated like this:
SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`=:qp1 INNER JOIN `auth_item` ON `auth_item`.`name`=:qp2 WHERE auth_item.name = :name
i appreciate any help
Try This Query
$query = (new yii\db\Query())
->select('id, username, auth_assignment.created_at')
->from('user')
->innerJoin('auth_assignment','user.id=auth_assignment.user_id')
->innerJoin('auth_item','auth_item.name = auth_assignment.item_name')
->where([
'auth_item.name' => 'Admin'
])->all();

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