Set a value of select in update query MYSQL - mysql

How can I use a result of select in update query? For example:
Update access_rights
set rfidcode=(Select rfidcode from users where name like 'thomas')
where id_access_rights=3;
This doesn't work. Can anyone help me ?

assuming you want to update a single record your select query needs to return a single result. use the limit keyword.
Update access_rights
set rfidcode=(Select rfidcode from users where name like 'thomas' limit 1)
where id_access_rights=3;
vkp solution if you want to update many related records but you have to watch your joins or you'll get errors, or worse corrupt your data

Update access_rights a, users u
set a.rfidcode = u.rfidcode
where a.userid = u.userid --change this to appropriate join column
and u.name like '%thomas%'
and a.id_access_rights=3;

Related

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 a value

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>
)

Not working an Update query as I wish

Hi I want to update a table with the values of another but do not how to do it.
I have tried this but it is not working.
UPDATE tblagendamiento SET
CodigoAgenda =
(select tmptable.CodigoCita from tmptable where tmptable.id = tblagendamiento.id);
And this is the error:
Subquery returns more than 1 row
You are actually getting an error because your subquery is returning more than one row. I think you can achieve this simply using an INNER JOIN query
UPDATE tblagendamiento a
INNER JOIN tmptable b
ON a.id = b.id
SET a.CodigoAgenda = b.CodigoCita
The message is telling you that there is more than one row returned by your subquery. Assuming you don't want to use a random value (which you can do by appending limit 1 to the query), it means your where clause is not selective enough.

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
)

How do I update the table(MYSQL)

the system throw a error "You can't specify target table 'screening' for update in FROM clause"
How do I completed the update like this?
UPDATE screening
SET maileddate = date('Y-m-d', strtotime($mailed_date[$screeningId]))
WHERE user_id IN (SELECT id FROM users
INNER JOIN screening ON
(users.id = screening.users_id
AND screening.id = {$screeningId}))
AND date BETWEEN 05-15/2011 AND 11-15-2011
LIMIT 2
You would have to remove the LIMIT option (similar to a TOP query in other SQL languages). You are basically saying find me the first 2 items and UPDATE. This may produce unexpected results on tables due to sort order.
Try putting the LIMIT clause in your SELECT subquery.