hibernate, mysql - mysql

i have the following hql query:
UPDATE TaskAssessment taskAssessment
SET taskAssessment.activeFlag = false
WHERE taskAssessment IN
(
SELECT taskAssessment2
FROM TaskAssessment taskAssessment2
Where taskAssessment2.activeFlag = true
AND taskAssessment2.patient.id
AND taskAssessment2.needsLevel.careNeed = :careNeed
)
but its giving me errors:
You can't specify target table 'TASK_ASSESSMENT' for update in FROM clause
could anyone help me to correct the query for mysql and hibernate. thanks in advance.

To resolve You can't specify target table 'TASK_ASSESSMENT' for update in FROM clause, rewrite the query to use JOIN instead of IN (in mysql you need to write something like this):
UPDATE TaskAssessment a
INNER JOIN TaskAssessment a2 ON (a2.id = a.id)
SET a.activeFlag = 0
WHERE a2.active_flag = 1 AND
a2.patient_id = :patient_id AND a2.needsLevel_careNeed = :careNeed

Related

Can't update two tables in one Query

I've got two Queries to Update two tables:
First Table
UPDATE user_info SET `location` = ".$locationid.", `looking_for` = ".$lookingfor." WHERE `user_info`.`user_id` = ".$infoid.";
Second Table
UPDATE user_personality SET `personality` = '".$changedescription."' WHERE `user_personality`.`user_info_id` = ".$infoid.";
And I'm trying to merge those two Queries, using the same statement.
UPDATE user_info, user_personality
SET user_info.location = ".$locationid.", user_info.`looking_for` = ".$lookingfor.", user_personality.personality = '".$changedescription."'
WHERE `user_info`.`user_id` = ".$infoid."
AND `user_personality`.`user_info_id` = ".$infoid."
I'm not receiving any error message, but is not updating.
What am I doing wrong?
Thanks.
Just a guess...
"
UPDATE user_info i
JOIN user_personality p
ON p.user_info_id = i.user_id
SET i.location = $locationid
, i.looking_for = '$lookingfor'
, p.personality = '$changedescription'
WHERE i.user_id = $infoid;
";
If you set the 2 table fields equal to each other in the where clause it should work, so I believe you'd change your where clause to:
WHERE `user_info`.`user_id` = `user_personality`.`user_info_id`
AND `user_info`.`user_id` = ".$infoid."
MySQL definitely supports updating multiple tables, so the where clause that works for a multi table select statement should also work for an update.

sql update with join

I am trying to update some rows in a table from another row in a different table.
this is the sql I have so far:
UPDATE nymb_posts
JOIN nymb_postmeta
ON nymb_postmeta.post_id = nymb_posts.ID
WHERE nymb_postmeta.meta_key = "_wp_attached_file"
AND nymb_posts.post_type = "attachment"
AND nymb_posts.post_parent = "0"
SET nymb_posts.Guid = nymb_postmeta.meta_value
I just get an "error in your SQL syntax".
If I remove the WHERE clause there is no error. If I make it a SELECT insead of an UPDATE the WHERE clause works.
What is wrong with the WHERE clause?
The set goes before the where:
UPDATE nymb_posts JOIN
nymb_postmeta
ON nymb_postmeta.post_id = nymb_posts.ID
SET nymb_posts.Guid = nymb_postmeta.meta_value
WHERE nymb_postmeta.meta_key = "_wp_attached_file" AND
nymb_posts.post_type = "attachment" AND
nymb_posts.post_parent = "0";
Use set before where clause then it will work

update query with case statement giving missing keyword error

There are 2 tables INFO and ADDR
ADDR has 4 columns ID, LINE_1_ADDR, LINE_2_ADDR, TEMP_ADDR_ID
INFO has 4 columns ID, FIRST_NAME, LAST_NAME, ADDR_ID
I want to replace the data in INFO.ADDR_ID with ADDR.ID whenever there is a match of INFO.ADDR_ID and ADDR.TEMP_ADDR_ID
I have the following UPDATE query which is giving error
ORA-00905: missing keyword
Below is my code:
UPDATE INFO SET INFO.ADDR_ID = (CASE
WHEN ADDR.TEMP_ADDR_ID = INFO.ADDR_ID
THEN INFO.ADDR_ID = ADDR.ID
END);
I am new to SQL queries involving case, don't know where it's wrong. Any help please !
Thanks
if you using MYSQL
UPDATE INFO
INNER JOIN ADRR ON ADDR.TEMP_ADDR_ID = INFO.ADDR_ID
SET INFO.ADDR_ID = ADDR.ID
If you using ORACLE
use this
UPDATE INFO SET INFO.ADDR_ID = (SELECT ADRR.ID
FROM ADRR
WHERE ADDR.TEMP_ADDR_ID = INFO.ADDR_ID)
EDIT: After getting more info, echo_Me's approach is the right one for the update.
UPDATE INFO
JOIN ADDR ON /*your Join condition*/
SET INFO.ADDR_ID = ADDR.ID

Multiply in update subquery select

I'm trying to update a table based on 2 select subquery that will be multipied to produce the value for Harga column
here is my code :
UPDATE bahanmakanan
SET Harga = (SELECT HargaSatuan from detail_bahanmakanan
WHERE IDBahanMakanan = "BM01")* (SELECT jumlah from bahanmakanan
WHERE IDBahanMakanan = "BM01")
WHERE IDBahanMakanan = "BM01" ;
The error message return
Error Code: 1093. You can't specify target table 'bahanmakanan' for update in FROM clause
you can simply do this using JOIN,
UPDATE bahanmakanan a
INNER JOIN detail_bahanmakanan b
ON a.IDBahanMakanan = b.IDBahanMakanan
SET a.Harga = a.jumlah * b.HargaSatuan
WHERE a.IDBahanMakanan = 'BM01'
Please do backup first your database before executing the statement.
Try this:
UPDATE bahanmakanan as t1
JOIN detail_bahanmakanan as t2 USING(IDBahanMakanan)
SET t1.Harga = t2.HargaSatuan * t1.jumlah
WHERE IDBahanMakanan = "BM01";

Update Query Issues

Here is my update query.
UPDATE sugarcrm.qb_salesorders_leads_c, sugarcrm.qb_salesorders
SET sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsleads_ida = sugarcrm.qb_salesorders.memo, sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb = sugarcrm.qb_salesorders.id
WHERE sugarcrm.qb_salesorders.id = sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
When I run this it gives me Affected rows 0.
Here is my select statement using the same info that is in the WHERE statement.
SELECT * from qb_salesorders_leads_c, sugarcrm.qb_salesorders
WHERE sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb = sugarcrm.qb_salesorders.id
This returns 354 Rows which is what I am expecting to update on the update query. What am I missing. Please help!
Convert the implicit join to an explicit one:
UPDATE sugarcrm.qb_salesorders_leads_c leads
INNER JOIN sugarcrm.qb_salesorders orders
ON orders.id = leads.qb_salesorders_leadsqb_salesorders_idb
SET leads.qb_salesorders_leadsleads_ida = orders.memo,
leads.qb_salesorders_leadsqb_salesorders_idb = orders.id
As you can see, I also used aliases to make the SQL compact and legible.
To UPDATE using a join, you have to use the explicit join syntax:
UPDATE sugarcrm.qb_salesorders_leads_c
INNER JOIN sugarcrm.qb_salesorders
ON qb_salesorders_leadsqb_salesorders_idb.id
= sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
SET sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsleads_ida
= qb_salesorders_leadsqb_salesorders_idb.memo
, sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
= sugarcrm.qb_salesorders.id