Not able to create a query on mysql - 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! :)

Related

SQL query for FIND and REPLACE with if condition

Is it possible to make a query like that so I get the desired result in MySQL?
I want to find and replace values: under table post_meta and in post_value ".../folder/..." to "..../events/...." if parent_post = = 342. Can any one help me?
Assuming you are looking for this
UPDATE post_meta
SET post_value = REPLACE(post_value, '/folder/', '/events/')
WHERE parent_post = 342

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

MySql Update where table_A.col_1 Between table_B.col_1 and table_B.col_2

can you help me with an sql query?
I want to get a value from another table but cannot use an inner join because the "join" column is not exactly the same in these two tables. Instead I must check the place where "Table_A.clubZipCode BETWEEN Table_B.zip_min AND Table_B.zip_max" holds.
This is my research efford so far:
UPDATE
Table_A
SET
Table_A.clubState = Table_B.state
FROM
clubs_data AS Table_A
JOIN zip_to_state AS Table_B
WHERE
Table_A.clubZipCode BETWEEN Table_B.zip_min AND Table_B.zip_max
However it draws an syntax-error on line 5.
Thank you!
Here is your query re-written with a valid syntax:
UPDATE clubs_data
SET clubState = (
SELECT state
FROM zip_to_state
WHERE clubs_data.clubZipCode >= zip_to_state.zip_min AND clubZipCode <= zip_to_state.zip_max
LIMIT 1
);
I do not know what you want to achieve, but generally the update syntax using JOIN would be something like this:
UPDATE clubs_data AS Table_A
JOIN zip_to_state AS Table_B
ON Table_A.clubZipCode >= Table_B.zip_min
AND Table_A.clubZipCode <= Table_B.zip_max
SET Table_A.clubState = Table_B.state;
EDIT: The solution just proposed by #Gab using a subquery might be better suited for you in this case.

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 update from one table to another with condition not working?

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.