mySQL update a value - mysql

Modified some stuff from my pic so you guys can understand it
I have this database. I am trying to update a value from a table based on another value from an another table.
I want to update the SUM from salary like this :
( sum = presence * 5 )
This is what I've been trying to use ( unsuccessful )
update table salary
set suma.salary = users.presence * 5
FROM salary INNER JOIN users1 INNER JOIN presence on id_salary = id_presence
I am not sure what to do, I'd appreciate some help, Thanks

In MySQL to UPDATE tables with a join you use this syntax:
UPDATE table1, table2
SET table1.column = some expression
WHERE table1.column = table2.column
That said, even with the updated picture, in your SQL you are mentioning columns that I cannot understand in which table are to be found. You also have an inner join between salariu and users1, with no join condition. Could you please clean up the question and make everything clear?
Assuming you are making the updates to the db structure you were talking about, then you can start working on this one maybe:
UPDATE salary, presence
SET salary.sum = SUM(presence.hours) * 5
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
Another way, but I'm not sure it is supported in all RDBMS, would be something like this:
UPDATE salary
SET sum = (
SELECT SUM(presence.hours) * 5
FROM user, presence
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
)

Related

mysql: I want to sum up all the values of credit from table semester and then add to another column total_credit from another table

I have the below code:
UPDATE VIEW
LEFT JOIN sem_view ON (view.semester = sem_view.semester)
SET view.t_credit = SUM(sem_view.credit)
but its not working saying invalid use of group
Data for updating must be perpared previously, in subquery. Updating must use the data which is already aggregated.
Left joining may be errorneous - it will set the value in the destination table to NULL if according data for the semester in interest in sem_view is not present. But if the logic needs this then you may use LEFT JOIN instead of INNER one in the below query.
Totally:
UPDATE `view`
JOIN ( SELECT sem_view.semester, SUM(sem_view.credit) summ
FROM sem_view
GROUP BY sem_view.semester ) data_for_updating
ON `view`.semester = data_for_updating.semester
SET `view`.t_credit = data_for_updating.summ;
PS. The text looks like view is a name of a table to be updated.

Update a MySQL table with index from another table based on value set in both tables

I am not sure how to ask this question - it might be answered somewhere, but it's kind of messy ;)
There are two tables that sort flags (labels) for objects somewhere else.
Table 1: the flags in booka_flags
columns of interest are id(INT) and title(VARCHAR)
Table 2: the flag items in booka_flags_items
columns of interest are id(INT) and flag(VARCHAR)
I want to change booka_flags_items.flag from VARCHAR (which is available from booka_flags.title) to ID (which is available from booka_flags.id)
My "idea" of writing this query (after some research) was the following:
UPDATE
booka_flags_items
SET
booka_flags_items.flag = booka_flags.id
FROM
booka_flags_items
INNER JOIN
authors
ON
booka_flags_items.flag = booka_flags.title
but it does not work. Also not working is this query:
UPDATE
booka_flags_items
SET
booka_flags_items.flag = (
SELECT booka_flags.id
FROM booka_flags
INNER JOIN booka_flags_items
ON booka_flags_items.flag = booka_flags.title
)
What would be the right way to solve a query like that?
I'm guessing you would like to update id column of booka_flags with id column of booka_flags where title matches flags. Your updated query looks incorrect as per mysql
Try:
UPDATE booka_flags_items t1 INNER JOIN booka_flags t2 On t1.flag = t2.title
SET t1.id = t2.id

Sql Select a minimum value from a table column and insert the results in another table column in one SQL statement

I am trying to get a minimum value from the Candidate table and insert that value in the MinTotal table. Can you do both in one SQL statement?
Here's my SQL Statement:
UPDATE MinTotal SET MinTotal.min_total= MIN(CandidateID.TotalVotes);
UPDATE MinTotal a
INNER JOIN (SELECT MIN(c.TotalVotes) min_vote, c.CandidateID FROM Candidate c
GROUP BY c.CandidateID) b ON b.CandidateID = a.CandidateID
SET a.min_total = b.min_vote;
Try the above. This is specific for each candidate, else you can use the other answers provided.
You have to use a select so you can properly set your MIN().
One way of doing that would be like that:
UPDATE MinTotal
SET
min_total = Cmin.minresult
FROM (
SELECT MIN(TotalVotes) as minresult
from CandidateID
) Cmin
In general, that would be one way to solve the Problem. In this case you would set the minresult for every row you have in your MinTotal table. If you dont want that, you may need to be more specific about your desired output and add some examples in your question
UPDATE MinTotal
SET MinTotal.min_total = (
SELECT MIN(TotalVotes)
FROM CandidateID
);

MySQL - Update Select Issues

The aim of my query is to use a value from one table A to look up a corresponding value from another table B, and then use that value to look up another value from another table C. This value should then be used to update a column in the original table A.
In my query so far I have managed to get a correct list of the values from table C using this command. In this command, Disbursements, Employee_Details & List_State_Codes correspond to tables A, B & C respectively as described above.
SELECT `List_State_Codes`.`Code`
FROM `List_State_Codes`
LEFT JOIN (
`Employee_Details` , `Disbursements`
) ON ( `Employee_Details`.`STATE` = `List_State_Codes`.`STATE` )
WHERE `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
This returns the correct list of values as required: One value from table C for each row in table A. Now my issue is to update the required column from table A with these returned values. This is where I am stuck.
The following query is what I believe to be my closest attempt:
UPDATE `Disbursements`
SET `Disbursements`.`CostCentreID` =
(
SELECT `List_State_Codes`.`Code`
FROM (SELECT * FROM `List_State_Codes`) AS `table`
LEFT JOIN (
`Employee_Details` , `Disbursements`
) ON ( `Employee_Details`.`STATE` = `List_State_Codes`.`STATE` )
WHERE `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
)
I receive the
error #1093 - You can't specify target table 'Disbursements' for update in FROM clause, despite adding the FROM (SELECT * FROM List_State_Codes) AS table line.
Thanks for any help.
You can join in the update, and this can help to remove the offending table from the subquery:
UPDATE `Disbursements`
JOIN `Employee_Details`
ON `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
LEFT JOIN `List_State_Codes`
ON `Employee_Details`.`STATE` = `List_State_Codes`.`STATE`
SET `Disbursements`.`CostCentreID` = `List_State_Codes`.`Code`
However, I could not test this, so let me know if this helps. (or if some other error occurs)
Thanks to Paul's help I have found the correct solution to my problem. The second join was what I needed as Paul suggested, along with an additional table alias to prevent the classic #Error 1903. I also needed to add the List_State_Codes (table C) to the JOIN to ensure it could access the correct data.
UPDATE `Disbursements`
JOIN `Employee_Details`
ON `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
LEFT JOIN (
`Employee_Details` d,
`List_State_Codes`
) ON
`Employee_Details`.`STATE` = `List_State_Codes`.`STATE`
SET `Disbursements`.`CostCentreID` = `List_State_Codes`.`Code`

subquery in where clause of UPDATE statement

I have the database of ATM card in which there are fields account_no,card_no,is_blocked,is_activated,issue_date
Fields account number and card numbers are not unique as old card will be expired and marked as is_block=Y and another record with same card number ,account number will be inserted into new row with is_blocked=N . Now i need to update is_blocked/is_activated with help of issue_date i.e
UPDATE card_info set is_blocked='Y' where card_no='6396163270002509'
AND opening_date=(SELECT MAX(opening_date) FROM card_info WHERE card_no='6396163270002509')
but is doesn't allow me to do so
it throws following error
1093 - You can't specify target table 'card_info' for update in FROM clause
Try this instead:
UPDATE card_info ci
INNER JOIN
(
SELECT card_no, MAX(opening_date) MaxOpeningDate
FROM card_info
GROUP BY card_no
) cm ON ci.card_no = cm.card_no AND ci.opening_date = cm.MaxOpeningDate
SET ci.is_blocked='Y'
WHERE ci.card_no = '6396163270002509'
That's one of those stupid limitations of the MySQL parser. The usual way to solve this is to use a JOIN query as Mahmoud has shown.
The (at least to me) surprising part is that it really seems a parser problem, not a problem of the engine itself because if you wrap the sub-select into a derived table, this does work:
UPDATE card_info
SET is_blocked='Y'
WHERE card_no = '6396163270002509'
AND opening_date = ( select max_date
from (
SELECT MAX(opening_date) as_max_date
FROM card_info
WHERE card_no='6396163270002509') t
)