MySQL update from one table to another with condition not working? - mysql

I have tried a solution that seems to work for someone else on this question:
Update table a from table b where (conditions)
I can not seem to get it working, MySql gives me a syntax error.
I have two tables, and I need to update a column in one table to the value of another column where an id matches in both tables.
UPDATE video_data SET video_data.date_timestamp = video.date_timestamp FROM video_data JOIN video ON video_data.video_id = video.video_id
I am not sure what the issue is with my syntax. I am quite tired and maybe it is just my eyes playing with me. Thanks for the help!

Try this syntax out:
UPDATE video_data, video
SET video_data.date_timestamp = video.date_timestamp
WHERE video_data.video_id = video.video_id

UPDATE video_data, video SET video_data.date_timestamp = video.date_timestamp
WHERE video_data.video_id = video.video_id

Try this:
UPDATE video_data SET date_timestamp = (SELECT video.date_timestamp FROM video WHERE video.video_id = video_data.video_id)
Although the error in your actual query is simply that you missed a "SELECT"
UPDATE video_data SET video_data.date_timestamp = SELECT video.date_timestamp FROM video_data JOIN video ON video_data.video_id = video.video_id
But I don't think it is what you want it to be.

Related

Not able to create a query on mysql

update email_template_mapping
set template_footer = (select id
from email_template_mapping
where template_description = 'footer'
);
this query giving me error You can't specify target table 'email_template_mapping' for update in FROM clause.
can you please help.
Thanks in advance.
In MySQL, you need to express this using JOIN:
update email_template_mapping etm left join
email_template_mapping etmf
on etmf.template_description = 'footer'
set etm.template_footer = etmf.id;
update email_template_mapping
set template_footer = id
where template_description = 'footer'
should look something like this, it looks like in that subquery youre using the same table so you can just set the template footer = id and then use the where after. Look up the format for an update statement and that will point you in the correct direction too, hope that helps! :)

Error Code 1242: Subquery retuens more than 1 row

I'm working on an update statement but I keep getting this error. Anyone have any advice on how to fix it. I've tried looking at solutions from similar questions for the past hour but can't seem to get them to work. Here's my sql statemtent:
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID);
** Update ur sql like this :**
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID AND ROWNUM = 1);
You have more more rows that match the conditions than you expect.
You can find the offending rows by doing:
select T_REGISTERED_UID, count(*)
from T_REGISTERED
group by T_REGISTERED_UID
having count(*) > 1;
If you just want a quick-and-dirty solution, use limit:
UPDATE T_SUBSCRIBERS s
SET FULLNAME = (SELECT CONCAT(T_REGISTERED_FNAME, T_REGISTERED_LNAME)
FROM T_REGISTERED r
WHERE r.T_REGISTERED_UID = s.T_SUBSCRIBERS_UID
LIMIT 1
);
In general, though, it is best not repeat column values like this in different tables. When you want the full name, just join to T_REGISTERED. After all, what happens if the user updates their registration name?

DELETE FROM table WHERE condition is met in other table

I have this SQL query that has been working great. I would like to have something similar that would delete a line from PRC_FIX when the column DESCR in IM_ITEM begins with Clearance instead of where ITEM_VEND_NO = 'GAMES WORK'.
DELETE `PRC_FIX` FROM `PRC_FIX`
INNER JOIN `IM_ITEM` ON `IM_ITEM`.`ITEM_NO` = `PRC_FIX`.`ITEM_NO`
AND `IM_ITEM`.`ITEM_VEND_NO` = 'GAMES WORK'
Thanks for your help.
Edit: This was marked as a possible duplicate. I don't know that looking at the suggested duplicate would have helped me because I wouldn't have known how to implement it in this scenario involving 2 tables, but I'm willing to admit that might be my fault due to me being new to SQL.
You can use
DELETE PRC_FIX
FROM PRC_FIX
INNER JOIN IM_ITEM
ON IM_ITEM.ITEM_NO = PRC_FIX.ITEM_NO
WHERE UPPER(IM_ITEM.DESCR) LIKE 'CLEARANCE%';
You need to use the wildcard %.
in order to match with this string with different string which begins with "Clearance" you need to use "Clearance%".
Look here: SQL like search string starts with
You're fixed code:
DELETE `PRC_FIX` FROM `PRC_FIX`
INNER JOIN `IM_ITEM` ON `IM_ITEM`.`ITEM_NO` = `PRC_FIX`.`ITEM_NO`
AND IM_ITEM.DESCR LIKE 'Clearance%'
DETELE FROM PRC_FIX WHERE ITEM_NO IN (SELECT ITEM_NO FROM IM_ITEM WHERE ITEM_VEND_NO` = 'GAMES WORK')

Please help: Multi-part identifier could not be bound

I know there are quite a few posts regarding this topic, but I'm really stumped as to why my code isn't working. I simply have two tables and am trying to update a value in one table with information from the other based on a where statement. I have pretty much used this same code in other instance to do this without any issue, so I'm not sure why I'm getting an error here in this case. If anyone could offer up any assistance, I'd greatly appreciate it.
Code:
update [DB].[dbo].[table1]
set [DB].[dbo].[table1].[variable_name] = [DB].[dbo].[table2].[variable_name]
where ([DB].[dbo].[table1].[Year] = [DB].[dbo].[table2].[Year] and
[DB].[dbo].[table1].[Id] = [DB].[dbo].[table2].[Id]);
Variable_Name is text in both tables and Year/Id are both Int.
You need to join table2 in the update statement.
UPDATE t1
SET [table1].[variabe_name] = [table2].[variable_name]
FROM [table1] t1
INNER JOIN [table2]
ON [table1].[variable_name] = [table2].[variable_name]
WHERE ([table1].[Year] = [table2].[Year] AND [table1].[Id] = [table2].[Id]);

MySQL, Why doesn't my update query work?

I'm made a copy of my MySQL database in MS Access as I was sure my query would work.
Heres my query
UPDATE Pads RIGHT JOIN Fix ON Pads.PadID = Fix.PadID
SET Pads.RemoveMeDate = '1999-01-01 00:00:00'
This query work in MS Access, but not in MySQL.
How do I fix this ? and why doesn't it work ?
EDIT
* When I say my query doesn't work, I mean no rows affected, when there are matching records ... *
I don't see a need for that join?
Try something like this:
UPDATE Pads
SET Pads.RemoveMeDate = '1999-01-01 00:00:00'
WHERE Pards.PadId IN (
SELECT PadId FROM Fix
)
UPDATE Pads, Fix
SET Pads.RemoveMeDate = '1999-01-01 00:00:00'
WHERE Pads.PadID = Fix.PadID
or solution above / below from Nanne depending what is a reason for JOIN
Try putting Pads.PadID = Fix.PadID in parenthesis
(Pads.PadID = Fix.PadID)
I've never actually tried doing a join on an update query, so I'm not sure if that will work.