I'm trying to update two tables in MySQL with one query, and am running into an error. I've looked at similar situations and resolutions, but can't seem to translate them to my specific query/situation as I am continuing to get the same error. My goal seems like there is an obvious solution I am missing. My Query is as follows:
UPDATE table_pr, table_pu
INNER JOIN table_pu ON table_pr.id = table_pu.pr_id
SET table_pr.cpr2 = table_pu.cpr2_id, table_pu.cpr_updated = '1'
WHERE table_pu.cpr2_date < CURRENT_DATE()
When I run the Update, I get Not unique table/alias: 'table_pu' returned. I prefer to not use aliases in this, but I've also tried to set unique aliases for the tables with the same result. If my approach should be modified, my ultimate goal is to have table_pr.cpr2 set to table_pu.cpr2_id based on WHERE table_pu.cpr2_date < CURRENT_DATE() and if the update is run, to also set table_pu.cpr_updated = '1'
Any help or guidance would be greatly appreciated.
Removing the second table reference in the UPDATE statement resolved the issue.
Related
Trying to update one table from a column in another table based on two conditions. I've tried many version of this but this is what I'm currently trying and it just hangs. When I check the table while running, I'm not seeing any updating occurring. Is there something wrong with my syntax? Thanks!
UPDATE earningsdates e
INNER JOIN actualmove a
ON e.ticker = a.theticker AND e.exactearningsdate = a.realearningsdate
SET e.theamove = a.themove;
I made ticker and theticker non-unique indexes and the query worked without a problem.
i have been trying to update a with the number of days using datediff.
The following statement works fine:
select DATEDIFF (date_stock_out,date_stock_in) from paddock
But when I try to update the third field in the table with the following statement, I am getting a syntax error:
update paddock set number_of_days = select DATEDIFF(date_stock_out,date_stock_in) from paddock
Where am I going wrong.
Thanks for any assistance
Normally you do an UPDATE without having a SELECT right in the middle of it:
UPDATE paddock SET number_of_days=DATEDIFF(date_stock_out,date_stock_in)
It's implied you're working on paddock so there's no need to repeat yourself.
SQL statements each have their own unique syntax and restrictions on how, where and when they can be used. Check carefully before composing them together like you would functions in other more flexible programming languages.
I have the following query:
update tblwerknemerdienst toUpdate
set datumtot = (select subdate(datumvanaf,1)
from (select * from tblwerknemerdienst) nextDienst
where nextDienst.Werknemer = toUpdate.Werknemer
and nextDienst.datumvanaf > toUpdate.DatumVanaf
order by DatumVanaf
LIMIT 1)
WHERE DatumTot IS NULL;
The query runs fine on MySql versions other than MySql 5.7.10. I've searched around the web and found that you have to set derived_merge=off, but sadly this had no effect and the query still fails with the same error.
I have also tried several different ways of rewriting the query, but all to no avail.
Is there something I'm missing or is there another way to accomplish this?
In the end I fixed this by rewriting the whole thing in a procedure, where I used a cursor to execute the query and get the necessary data. Then I perform the update statement based upon the fields selected in the cursor.
This seemed to be the only way to reliably perform the operation required on different versions of MySql.
I am trying to execute an UPDATE statement as follows:
UPDATE table_name
SET goal_seq = 'xyz'
WHERE league_id = 20
AND home = 0
AND away = 0;
In trying to work out why it wasnt working I have established that you cant have multiple WHERE clauses (is that correct?) but I cant figure out how to get round it.
Any help much appreciated,
P
you CAN have multiply conditions after WHERE statement - go to Mysql docs for more information and some examples - i believe the key table in your code is just a replacement for actual table name? If this query doesn't work pls provide the error which server returns - without it we cannot help you.
I want to log the full SQL query of anything changing the value of one column from one pre-determined value to another pre-determined value. Unfortunately, it is unclear to me how to do this, if it even is at all.
Any ideas?
Thanks!
Set up an update trigger (after or before) on the table.
Then have logic (expressed as pseudo-code):
if old.col = 'Value1' and new.col = 'Value2'
then insert into logrecords(. . .) select "what I want to log here"
end if ;
MySQL Proxy can help you to get the query and you can write a filter to select queries only that are affecting specific rows.