How to get my own Mysql Query work? - mysql

Can please someone tell me why this query is not working in MySQL....Or how to get it work?
UPDATE table1, table2 SET abc = 0 FROM table2
WHERE table2.xyz > 0 AND table1.id_x = table2.id_x
Thank You

You appear to want a JOIN in an UPDATE. The correct syntax in MySQL is:
UPDATE table1 JOIN
table2
ON table1.id_x = table2.id_x
SET abc = 0
WHERE table2.xyz > 0;
Your query does not work for basically two reasons:
MySQL does not support FROM in the UPDATE statement (although SQL Server and Postgres do).
You have used table1 without defining it.

Related

Coldfusion ORM update with Join

I am looking for help on a HQL-statement, where I would like to do an update with a JOIN. I tried following statement, which runs fine in MySQL-Workbench:
UPDATE table1 t1
SET t1.status='Running'
JOIN t1.table2 t2
WHERE t1.status='Open' AND t2.destination ='mydestination' "
but it gives the error:
expecting "set", found 'JOIN' near line 1, column 16
It seems to me that as this is a bulk update operation then you should be using SQL for this not HQL. ORM is not really designed for this type of update. You can always run the SQL statement and then if you need to, load the object graph (your ORM entities) after.
As for the SQL you'll need to run it using cfquery (or the cfscript equivalent), from your HQL example it'd look something like this as SQL (assuming MySQL as you mention MySQL Workbench)
<cfquery>
UPDATE table1 t1
INNER JOIN table2 t2 ON t1.col = t2.col
SET status='Running'
WHERE status='Open'
AND t2.destination='mydestination'
</cfquery>
If you want to do it with HQL then you usually need to use a subquery. Something like this:
update table1 t1
set t1.status = 'Running'
where t1.state = 'Open'
and t1 in (
select t2.table1
from table2 t2
where t2.destination='mydestination'
)

MySQL UPDATE INNER JOIN with AES_DECRYPT

UPDATE table_one a
INNER JOIN table2 b on AES_DECRYPT(a.username, '$key') = AES_DECRYPT(b.username, '$key')
SET a.unit = b.unit, a.unitb = b.unitb;
Problem: Need to update table_one with the new units from table_two.
Question 1: I've ran this SQL and it seems to not be updating the unit field in table_one. Is my syntax incorrect for that?
Question 2: Is there a better way to compare the username's since they are encrypted (Yes, they have to stay encrypted in the database)?
Thank you in advance.

operation must use an updateable query - access

I want to update a table by using the join in access - 2007
UPDATE TABLE1 A INNER JOIN (SELECT ACCODE, SUM(AMOUNT) AS SUM_AMOUNT
FROM TABLE2 GROUP BY ACCODE) B ON A.ACCODE = B.ACCODE
SET A.TRIAL = A.TRIAL + SUM_AMOUNT
it gives me error that
operation must use an updateable query
i have try with below query and no error is here
UPDATE TABLE1 A INNER JOIN TABLE2 B ON A.ACCODE = B.ACCODE
SET A.TRIAL = A.TRIAL + SUM_AMOUNT
please help me to find what is wrong with first query
I think the reason Access treats your query as non-updateable is due to the subquery GROUP BY. You should be able to create an updateable query by using DSum.
UPDATE TABLE1 AS a
SET a.TRIAL = a.TRIAL
+ DSum("AMOUNT", "TABLE2", "ACCODE=" & a.ACCODE)
If ACCODE is text instead of numeric data type, add quotes around the value in the DSum expression.
UPDATE TABLE1 AS a
SET a.TRIAL = a.TRIAL
+ DSum("AMOUNT", "TABLE2", "ACCODE='" & a.ACCODE & "'")
I just had this issue and it was due to permissions on the table. I made a copy of the linked table to test my update query on, and kept getting this error message. I was pretty sure my query would be ok (it was a simple update) so I ran it on the actual table and I didn't get the error. Just wanted to add this for anyone else that comes across this in the future!
this error also comes when you are accessing database which is on different PC. so give write permission from security tab of folder and also give write access on sharing permission option.
In my case this is worked
Yes Mandeep, that is the way ms-access works.
UPDATE Table1 As T1 INNER JOIN Table2 AS T2 ON T1.F1 = T2.F1
SET T1.F2 = T2.F2
This query raises 'operation must use an updateable query' erro when T2 is a query, even when you are not updating T2.

How does mysql delete syntax differ from select?

This:
SELECT *
FROM tbl_playlists, tbl_playlistsongs
WHERE tbl_playlists.playlist_id = tbl_playlistsongs.playlist_id
AND tbl_playlists.playlist_id = 1
...works no problem. But:
DELETE from tbl_playlists, tbl_playlistsongs
WHERE tbl_playlists.playlist_id = tbl_playlistsongs.playlist_id
AND tbl_playlists.playlist_id = 1
...says I have a syntax error. They're identical other than the SELECT * vs DELETE. It still makes perfect logical sense to me.. but I must be missing something!
Traditional SQL doesn't support multi-table deletions, but MySQL does. That means you're using MySQL specific syntax:
DELETE pl, pls
FROM TBL_PLAYLISTS pl
JOIN TBL_PLAYLISTSONGS pls ON pls.playlist_id = pl.playlist_id
WHERE pl.playlist_id = 1
Reference:
DELETE (MySQL documentation)

Self-referencing updates in HQL

I have the following query in HQL:
update ProjectFile pf1
set pf1.validUntil.id =123
where pf1 = (
select pf from ProjectVersion pv, ProjectFile as pf
where pf.validFrom.sequence <= pv.sequence
and pf.validUntil.sequence >= pv.sequence
and pf.state <> 12
and pf.projectVersion.project.id = 1
and pv.project.id = 1
and pv.id = 12
and pf.id not in (2,3,4)
)
Hibernate parses the query correctly and generates SQL, but the database (MySQL) fails with error:
You can't specify target table 'ProjectFile' for update in FROM clause
The problem seems to be that the table to be updated is queried in the same context. Is there any way to rewrite the HQL query to produce SQL that can be executed in MySQL correctly? The other approach would be to create an intermediate table, which is what exactly I am trying to avoid.
I bumped into the same problem and posted a question here: MySQL/SQL: Update with correlated subquery from the updated table itself.
To solve your problem, you need to join at the UPDATE level, please take a look at the answer to my question.