This question already has answers here:
Swapping column values in MySQL
(23 answers)
Closed 8 years ago.
i am wanting to try and run a query which will update my table ptb_messages and switch the data of two columns around, for instance heres my table:
id | to_user_id | from_user_id |
1 4 5
2 5 6
3 7 9
so what i want to try and do is switch the value of from_user_id over to to_user_id and vice versa.
i was going to use this before i realised that once i copied the value from one table to the other the the original data from the other column would then have been overwritten,
$sql = mysql_query("UPDATE ptb_messages SET ptb_messages.from_user_id=ptb_messages.to_user_id");
what i really need is a swap function,
im not really sure how i might do this but im imagining its something like this:
$sql = mysql_query("UPDATE ptb_messages SET to_user_id=from_user_id, from_user_id=to_user_id");
hope someone can help me please.
Well, mysql has a concept called user variable. You can take advantage of this to store the value and set it on the column for swapping,
UPDATE Table1
SET to_user_id = from_user_id,
from_user_id = #r1
WHERE #r1 := to_user_id
see here: http://www.sqlfiddle.com/#!2/8cd6a/1
how about joining the table with itself?
UPDATE Table1 a
INNER JOIN Table1 b
ON a.to_user_id = b.to_user_id AND
a.from_user_id = b.from_user_id
SET a.to_user_id = b.from_user_id,
a.from_user_id = b.to_user_id
see here: http://www.sqlfiddle.com/#!2/d6b4f/1
I'm going to throw an answer out there, although I think Skinny Pipes answer is a little more elegant.
You can create a temporary table to store the values in.
http://www.sqlfiddle.com/#!2/3631d/1
create table ptb_messages_temp (
id int,
tempid int);
insert into ptb_messages_temp (id, tempid) (select id, to_user_id from ptb_messages);
-- DO THE UPDATE
UPDATE ptb_messages
LEFT OUTER JOIN ptb_messages_temp on ptb_messages.id=ptb_messages_temp.id
SET
ptb_messages.to_user_id=ptb_messages.from_user_id,
ptb_messages.from_user_id=ptb_messages_temp.tempid;
Related
This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 2 years ago.
I went through the previous answer like this, but it gives me the following error : You can't specify target table 'table_name' for update in FROM clause.
I have a table with say 3 columns (id -> auto increment primary id) :
id, roll_no and attendance
And for selected roll numbers having many entries each, except the first entry I want to update all entry attendance field as P.
The query which I wrote is following :
UPDATE tbl_class_attendance
set attendance = 'P'
where id NOT IN
(Select min(id)
from tbl_class_attendance
WHERE roll_no IN ('25', '45', '55')
GROUP
BY roll_no;
But it gives me the above error.
I also went through other answers asking to use two select queries but there the answer I didn't find completely easy to understand as well as difficulty in executing for my selected list of roll numbers.
So, is there a way to update?
EDIT : Answer given below
UPDATE tbl_class_attendance t1
LEFT JOIN ( SELECT t2.roll_no, MIN(t2.id) id
FROM tbl_class_attendance t2
WHERE t2.roll_no IN ('25', '45', '55')
GROUP BY t2.roll_no ) t3 USING (roll_no, id)
SET t1.attendance = 'P'
WHERE t3.id IS NULL;
Got it working by following :
Update table SET a=value WHERE x IN
(Select * from (select x from table where condition) as t)
Credit : https://stackoverflow.com/a/43610081/6366458
I am trying to update a value to be NULL where tracker_unique_id can be found in ab_split_tracker_unique_link where that ones ab_split_id can be found in a 3rd table ab_split_candidate.
I cant do it by giving it different values as they can be different from user to user on locals
UPDATE trustpilot_links SET `invite_after_enquiry` = NULL
WHERE EXISTS (
SELECT tracker_unique_id, ab_split_tracker_unique_link.ab_split_candidate_id
FROM ab_split_tracker_unique_link
WHERE EXISTS (
SELECT ab_split_candidate_id
FROM ab_split_candidate LEFT JOIN ab_split
ON ab_split_candidate.ab_split_id = ab_split.ab_split_id WHERE ab_split.reference="review_invite_after_enquiry"
)
);
Edit:
Table examples
Trustpilot Links
trustpilot_link_id | invite_after_enquiry | tracker_unique_id
1 1 123
2 0 1234
ab_split_tracker_unique_link
tracker_unique_id | ab_split_id
1234 32
Ab Split
ab_split_id | reference
32 review_invite_after_enquiry
I want to set values to null if there tracker cannot be found in table ab_split_tracker_unique_link with an ab_split_id that is equal to review_invite_after_enquiry in ab_split
Your subqueries are not related to their parent queries as they should be. Let's look at your inner-most query:
SELECT ab_split_candidate_id
FROM ab_split_candidate
LEFT JOIN ab_split ON ab_split_candidate.ab_split_id = ab_split.ab_split_id
WHERE ab_split.reference = 'review_invite_after_enquiry'
Well, first of all your WHERE clause dismisses outer-joined records, so this is essentially an INNER JOIN. But then: either there are such records or not. This has nothing to do with the record your are potentially updating, nor with the ab_split_tracker_unique_link you are looking up.
So either you are updating all records or none.
We would rather expect something like
UPDATE trustpilot_links tl
SET invite_after_enquiry = NULL
WHERE EXISTS
(
SELECT *
FROM ab_split_tracker_unique_link stul
WHERE stul.tracker_unique_id = tl.tracker_unique_id
AND ...
);
So add WHERE clauses that relate the subqueries to their parent queries.
I'm trying to make an query that checks if the field 'field_A' in 'Table_A' is '0' and if it is, update 'field_B' in 'Table_B' to '10'.
I have no idea if this is even possible. I have very little experience with mysql and I'm just trying some things out. But if it is, I'd like to know how this is done.
update table_B
set field_B = 10
where exists (select 1 from table_A where field_A = 0)
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>
)
This is based on my previous question.
I have the following table
Table1
JobPositionId | JobPositionName
1 | Sound
2 | Lights
3 | Sound
4 | Ground
How can I delete row three (Name = sound, and max position)
Use:
DELETE FROM TABLE t1
JOIN (SELECT x.jobpositionname,
MAX(jobPositonId) AS max_id
FROM TABLE x
GROUP BY x.jobpositionname) t2
WHERE t1.jobPositonId = t2.max_id
AND t1.jobpositionname = t2.jobpositionname
AND t2.jobpositionname = 'Sound'
As I mentioned in your previous question, using this won't trigger a MySQL 1093 error:
DELETE FROM TABLE
WHERE JobPositionId = SELECT x.id
FROM (SELECT MAX(JobPositionId) AS id
FROM TABLE
WHERE JobPositionName = 'Sound') x
DELETE FROM
Table1
WHERE
JobPositionId = (
SELECT
MAX(JobPositionId)
FROM
Table1
WHERE
JobPositionName = 'Sound'
)
Sorry if this doesn't take into account your "previous question" thought I'd just look at this one.
DELETE FROM Table1 WHERE jobpositionid = (SELECT MAX(jobpositionid) FROM table1 WHERE name = 'Sound');
It seems like what you are trying to do is to delete all duplicate JobPositionNames, leaving only the one with the lowest JobPositionId.
I had to do something very similar recently and found the SQL statements getting so complicated, it was much much easier (if much less efficient) to do it in SQL.
So in case this is a poluted database you're trying to clean, just write a script that does this and be done with it (and set some unique indexes to prevent it from happening again).
If this happens all the time, and needs to be done periodicaly, fix the code that does this.