Not working an Update query as I wish - mysql

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.

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 Conditional Update in same table

I'm looking for a simple way to do an update on a table only if there is no other columns present in that same table with the same value I'm trying to update, ideally in a single query. So far I'm getting an error You specify target table 't1' for update in FROM clause. Here is what I tried in a few variations so far (still unable to get working):
UPDATE emailQueue AS t1
SET
t1.lockedOn = 1470053240
WHERE
(SELECT
COUNT(*)
FROM
emailQueue AS t2
WHERE
t2.lockedOn = 1470053240) = 0
AND t1.lockedOn IS NULL
In MySQL, you need to use a join. In this case, a left join is in order:
UPDATE emailQueue eq LEFT JOIN
emailQueue eq2
ON eq2.lockedOn = 1470053240
SET eq.lockedOn = 1470053240
WHERE eq.lockedOn IS NULL AND
eq2.lockedOn IS NULL;

Target table not allowing itself to be derived from a query on itself

I get the an error for the below MySQL.
Error Code: 1093. You can't specify target table 'songlist' for update in FROM clause
I think I need to be using inner join, but can't seem to make it work with my WHERE clause nested in the select statement. Any ideas?
SET SQL_SAFE_UPDATES=0;
UPDATE samdb.songlist
SET
songlist.xfade = (SELECT
t.xfade
FROM
tmpsonglist AS t,
songlist AS s
WHERE
t.album = s.album)
WHERE
filename LIKE '%201501.mp3';
SET SQL_SAFE_UPDATES=1;
The desired outcome is to update column values of xfade in songlist with tmpsonglist, as long as the filename field ends in 201501.mp3 (based on a common album field).
Try the following:
SET SQL_SAFE_UPDATES=0;
UPDATE samdb.songlist AS s
INNER JOIN samdb.tmpsonglist AS t
ON t.album = s.album
SET
s.xfade = t.xfade
WHERE
s.filename LIKE '%201501.mp3';
SET SQL_SAFE_UPDATES=1;
You do not need a nested query because the join involves the table to be updated. You can simply use an inner join and update only the required table by using the reference.

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

update field with another field another table

I want to update a field in table1 with another field in table2.I wrote the following query but it is not working.
UPDATE tempdata A
SET A.new_user_id =
(SELECT B.id FROM user B
WHERE A.usr_email = B.usr_email)
It is giving "#1242 - Subquery returns more than 1 row" error. Anybody please help me.
UPDATE tempdata A, user B
SET A.new_user_id = B.id
WHERE A.usr_email = B.usr_email
you can still join tables even if it is an update statement.
UPDATE tempdata A
INNER JOIN user B
ON A.usr_email = B.usr_email
SET A.new_user_id = B.id
Beware that error means that in table user there are more than 1 rows with field usr_email equals to tempdata's one. Check for dupes before runniing the actual update statement with the LIMIT 1 a suggested by Salil
This is also one way to handle this scenerio
UPDATE A
SET A.new_user_id = B.id
FROM tempdata A
INNER JOIN user B ON A.usr_email = B.usr_email