How to update the each record in sql server - mysql

I need to update each records in the table using the Update script
query 1:
Select acheivementsId
from Students
where student_id = 2
Result:: 61 records// Number of acheivementsid
query 2 :
Select acheivementsId
from Students
where student_id = 4
Result: 61 records// Number of acheivementsid
I need to update Student_id = 2 acheivementid's with student_id = 4 acheivementId's.
how to write the Update Statement for updating 61 records.
Thanks

Try this:
UPDATE Students
SET acheivementsId = (SELECT acheivementsId from Students where Student_id=4)
WHERE Student_id=2

Related

MySQL Trigger Setting All Other Values to NULL When Run

I have two tables, Accounts and Person:
CREATE TABLE Person(
id INT NOT NULL PRIMARY KEY,
Person_Name VARCHAR(17) NOT NULL,
P_Location INT NOT NULL
);
INSERT INTO Person VALUES (1,"Adam",300),(2,"Betty",10),(3,"Louis",60);
CREATE TABLE Accounts(
Person_id INT PRIMARY KEY,
Balance INT DEFAULT 200);
INSERT INTO Accounts VALUES (1,2000),(2,1350),(3,800);
And one trigger, Bonuses:
CREATE TRIGGER Bonuses
AFTER UPDATE ON Person
FOR EACH ROW
UPDATE Accounts
SET Balance = CASE WHEN (SELECT P_Location FROM Person WHERE id = Person_id) = 3 THEN Balance - 150
WHEN (SELECT P_Location FROM Person WHERE id = Person_id) = 7 THEN Balance + 100
WHEN (SELECT P_Location FROM Person WHERE id = Person_id) = 15 THEN Balance - 30
WHEN (SELECT P_Location FROM Person WHERE id = Person_id) = 1 THEN Balance + 200
END;
And I want to make the trigger update the Accounts table according to certain instructions whenever the P_Location on the Person table changes to one of a select few values (3,7,15 and 1). However, as things are they result is incorrect. Assume I run the above code, the tables I get are:
Person
id
Player_Name
P_Location
1
Adam
300
2
Betty
10
3
Louis
60
Accounts
Person_id
Balance
1
2000
2
1350
3
800
Now if I run UPDATE Person SET P_Location = 3 WHERE id = 1; then the Accounts table should yield:
Person_id
Balance
1
1850
2
1350
3
800
However, what I get is
Person_id
Balance
1
1850
2
NULL
3
NULL
Any idea of what I'm doing wrong?
Well, that code did exactly what you said, though it wasn't what you meant!
That's the thing about UPDATE queries, EVERY row will get an update unless a WHERE clause is used to filter what actually gets modified. Nothing is found from the CASE with most records, so any of those will get assigned to NULL. To see this behavior, check this fiddle example.
However, there is good news, all that is needed in the trigger is to add a WHERE clause. Note that I simplified the CASE handling make use of the UPDATE trigger's NEW references:
CREATE TRIGGER Bonuses
AFTER UPDATE ON Person
FOR EACH ROW
UPDATE Accounts
SET Balance = CASE WHEN NEW.P_Location = 3 THEN Balance - 150
WHEN NEW.P_Location = 7 THEN Balance + 100
WHEN NEW.P_Location = 15 THEN Balance - 30
WHEN NEW.P_Location = 1 THEN Balance + 200
END
WHERE Person_id = NEW.id;
So starting with:
Then run: UPDATE Person SET P_Location = 3 WHERE id = 1;
Gives:
Example fiddle with your tables, the simplified trigger case handling, and the output examples from the update query.

SQL update where select from the same table [duplicate]

I have a table. I want to update the 5th row with 10th row values from the same table. For example:
SlNo Name Quali Exp
1 x B.E 2
2 y BSC 3
3 Z B.A 1.5
4 A MSC 2
5 B MBA 5
Here i want to update second row with the value of 5th row.
Here is my current query:
UPDATE table
SET Name=(select Name from table where slNo='5'),
Quali=(select Quali from table where slNo='5'),
Exp=(select Exp from table where slNo='5')
where slNo='3';
this is working fine ... but if there are more than 20 columns it becomes laborious to write a query this way, because for each column I have to include another sub-query... is there any other way to write query to update the whole row with all values from the other row in the same table?
Use a self-join with the multiple table UPDATE syntax:
UPDATE `table` AS t1 JOIN `table` AS t2 ON t2.slNo = 5
SET t1.Name = t2.Name, t1.Quali = t2.Quali, t1.Exp = t2.Exp
WHERE t1.slNo = 3

How do I update multiple columns in a table with multiple conditions in MySQL?

Is it possible to run an update query on multiple columns with multiple conditions in MySQL?
id name value price instock pp_flag
1 xyz 23 27 1 9
2 abc 28 12 0 8
For example above is the structure of a table myTable, where I want to run a query like:
UPDATE TABLE myTable
set value = 25
where id = 1
and price = 12
where pp_flag = 8
Just wondering if I can do this in the same query in MYSQL.
Thanks
Use and in where clause:
UPDATE TABLE myTable
set value = 25
where id = 1
and price = 12 and pp_flag = 8
What I understand is that you would like to do this
UPDATE TABLE myTable set value = 25 where id = 1
and
UPDATE TABLE myTable set price = 12 where pp_flag = 8
in a single statement.
You cannot do this as these are two independent WHERE-conditions.
You can use multiple condition to update column of a table .
As per your requirement you can use below query:
UPDATE TABLE myTable set value = 25
where id = 1 or (price = 12 and pp_flag = 8);
Hope it will help you!
Yes, it is possible by using the inbuilt IF function in MySQL (https://dev.mysql.com/doc/refman/8.0/en/if.html).
Query for your example would be:
UPDATE myTable SET
value = if(id=1, 25, value),
price = if(pp_flag=8, 12, price)

How to update one table in MYSQL from another table?

I have two tables
Table tool
column names:
id toolnumber currentduedate
1 123 11/3/2015
2 456 11/3/2015
3 789 11/3/2015
Table event
column names:
id eventnumber newDuedate
7 123 11/3/2015
9 123 11/3/2015
10 456 11/3/2015
What i want is when i update the newDuedate in table event it should update the currentduedate in tool table.
I am using this query:
mysql_query
UPDATE tool INNER JOIN event SET tool.currentduedate = event.newDuedate WHERE tool.toolnumber = event.eventnumber ;
is working fine but if i have 2 field with the same eventnumber this query update only one. Any ideas?
Try this way
UPDATE tool
INNER JOIN event on tool.toolNumber = event.eventnumber
SET tool.currentduedate = event.newDuedate ;

Mysql update column

I have 2 tables fullInfo and fundInfo. fullInfo is a full data set of donations to a non-profit. fundInfo is a list of unique fund subgroups with accompanying id numbers. I'm trying to insert the fund id number from fundInfo into fullInfo in a column fundId that exists but currently has NULL values.
fullInfo:
id funddesc amount fundId
002 GENERAL 25.00 NULL
044 MAINT 50.00 NULL
122 TRAVEL 75.00 NULL
... ... ... ...
fundInfo:
id funddesc
01 MAINT
02 TRAVEL
03 GENERAL
... ...
update fullInfo
set fullInfo.fundId = fundInfo.id
where fullInfo.funddesc = fundInfo.funddesc;
This code is not working. Any suggestions?
update fullInfo
set fullInfo.fundId = fundInfo.id
from
fundInfo
where fullInfo.funddesc = fundInfo.funddesc;
UPDATE fullInfo
INNER JOIN fundInfo
ON fullInfo.funddesc = fundInfo.funddesc
SET fullInfo.fundId = fundInfo.id;