MySQL UPDATE INNER JOIN with AES_DECRYPT - mysql

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.

Related

SQL How to Update by INNER JOIN -

Please help me figure this out since I tried everything from this forum but still haven't found a solution.
Well, I have two tables:
prices
manufacturers
I want to change the values of two fields that are both in table prices.
And I will just give specific values to those.
The fields are:
prices.override (in which I want to give the value 0) and
prices.product_discount_id (in which I want to give the value 66)
BUT I want to change the fields ONLY FOR the manufacturer with ID 31.
So, I first check that an INNER JOIN works fine.
SELECT manufacturers.manufacturer_id,
prices.product_id,
prices.product_price,
prices.override,
prices.product_discount_id
FROM manufacturers
INNER prices
ON manufacturers.product_id=prices.product_id
AND manufacturers.manufacturer_id=31;
But when I try to update the two fields, I do not know how to make that work.
For example, I tried this but it didn't work:
UPDATE prices
SET prices.override=1
FROM
INNER JOIN prices
ON manufacturers.product_id=prices.product_id
AND manufacturers.manufacturer_id=31;
I also tried this:
UPDATE prices
SET prices.override=1,
INNER JOIN manufacturers
ON prices.virtuemart_product_id = manufacturers.virtuemart_product_id
AND manufacturers.manufacturer_id=31;
What did i do wrong? Usually the error message I get is:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM jos_virtuemart_product_prices prices INNER JOIN jos_virtuemart_product_man' at line 3
I read something for alias but still no result.
Any help would be appreciated!
You have a few syntax problems , MySQL UPDATE..JOIN syntax is :
UPDATE T
JOIN t2 ON()
SET ..
WHERE ..
Secondly, you had an unnecessary comma after the SET prices.override=1, so:
UPDATE prices
INNER JOIN manufacturers
ON prices.virtuemart_product_id = manufacturers.virtuemart_product_id
AND manufacturers.manufacturer_id=31
SET prices.override=1
Try this if you are using SQL Server for mysql above is fine:
UPDATE p SET p.override=1
FROM prices p
INNER JOIN manufacturers ON
p.virtuemart_product_id =manufacturers.virtuemart_product_id
AND manufacturers.manufacturer_id=31;
This is the correct syntax in MySQL:
UPDATE prices p JOIN
manufacturers m
USING (product_id)
SET p.override=1
WHERE m.manufacturer_id = 31;
Note the use of table aliases. These make a query easier to write and to read.
The syntax that you are using is appropriate for SQL Server.
In MySQL the UPDATE with JOIN syntax is different, the SET operator should comes after the JOIN statement.
The correct UPDATE query is
UPDATE prices P
INNER JOIN manufacturers M ON P.virtuemart_product_id = M.virtuemart_product_id
AND M.manufacturer_id = 31;
SET P.override = 1;
UPDATE prices
SET prices.override=1
FROM manufacturers
INNER JOIN prices
ON manufacturers.product_id=prices.product_id
AND manufacturers.manufacturer_id=31;

MySQL Error You can't specify target table for update in FROM clause

I have a 2 tables MST_customer and TRN_sales in my database with corrupt entries. The next query returns the corrupt entries:
SELECT TRN_sales.cust_no
FROM MST_customer
RIGHT OUTER JOIN TRN_sales
ON MST_customer.cust_no = TRN_sales.cust_no
WHERE MST_customer.cust_name IS NULL;
I tried to delete them executing:
DELETE FROM mydbB.TRN_sales
WHERE TRN_sales.cust_no IN (
SELECT TRN_sales.cust_no
FROM MST_customer
RIGHT OUTER JOIN TRN_sales
ON MST_customer.cust_no = TRN_sales.cust_no
WHERE MST_customer.cust_name IS NULL
);
But I get the next error:
You can't specify target table 'TRN_sales' for update in FROM clause
How can I resolve this problem ?
To be a bit more on "the safe side" you should specifiy the table (here: alias name s) you want to delete from like:
DELETE s FROM TRN_sales s
LEFT JOIN MST_customers ON MST.cust_no=TRN.cust_no
WHERE MST.cust_name IS NULL;
Personnaly I believe, this LEFT JOIN is easier to read, although of course you can do the same with your RIGHT JOIN version.
Why you don't try below-
DELETE TRN.*
FROM MST_customer MST
RIGHT OUTER JOIN TRN_sales TRN
ON MST.cust_no = TRN.cust_no
WHERE MST.cust_name IS NULL;
Note: For safe side keep backup of both tables before executing this query.

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.

Mysql syntax help

Hi i am new to mysql and i have made the following statement
UPDATE parent, chill SET parent.iod=chill.iod WHERE parent.id=chill.id
what i want to do is copy all the iod's from chill to parent where id should be matched.
But the result is that all the iod updated are set to 11 rather than iod's in chill.
iod's is of type
BIGINT
Is there ant mistake in the syntax or anything that i am doing wrong?
help with little explanation appreciated.
mysql update syntax for join tables:
The preceding example shows an inner join that uses the comma operator, but multiple-table UPDATE statements can use any type of join permitted in SELECT statements, such as LEFT JOIN.
try the following (although your example should work as well):
UPDATE parent
INNER JOIN chill
ON parent.id = chill.id
SET parent.iod = chill.iod

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.