I have written this query
set sql_safe_updates=0;
delete from t1 where id < (select avg(id) from t1);
set sql_safe_updates=1;
but it giving an error
Error Code: 1093. You can't specify target table 't1' for update in FROM clause
please resolve.
You can't specify the same table used in the delete in the rest of the query. One way to solve this is using a join:
delete t
from t1 t join
(select avg(id) as avgid
from t1
)
on id < (select avg(id);
This is a limitation in MySQL. Another way to get around it is to use an additional level of subqueries:
delete from t1 where id < (select avgid from (select avg(id) as avgid from t1));
The reason this works is because MySQL will materialize the subquery. The compiler sort of "forgets" that it is querying from the table bing modified, because of the extra level of subqueries.
Related
I got this error on select statement in MySQL.
First one without error :
select '2018-09-01' as rdate,
model,
part_no,
sum(part_cost*count(*))
from table1
left join table2 on table1.part_no = table2.part_no
where issue > '2018-09-01'
and issue_date <= '2018-09-30'
group by model, table1.part_no
2nd cause
1022 - Can't write; duplicate key in table '' (it is blank)
select '2018-09-01' as rdate, model, part_no, sum(part_costcount()) from table1 left join table2 on table1.part_no = table2.part_no where issue_date<='2018-09-30' group by model, table1.part_no
table1 have index on model,part_no and table1 have index on part_no.
I just remove issue > '2018-09-01'.
Can anyone help?
Finally, I simplify my query, actually I also use ' upper (model) as model' then try with model only, and it works. I just wonder why the error message doesn't pinpoint the cause of error. Of cause the real cause of error is still a mystery.
I am trying to count distinct values after Grouping by id then inserting them into another table with the following query:
INSERT INTO table_aggregate
(id_aggregate, aggregate_column)
(SELECT id_detail, COUNT(DISTINCT(detail_column))
FROM table_detail
GROUP BY id_detail)
ON DUPLICATE KEY UPDATE
aggregate_column = COUNT(DISTINCT(detail_column));
When run I get the error:
ERROR 1111 (HY000): Invalid use of group function
If I run the SELECT statement portion of the query it works fine. Why is it throwing this error?
You can not use COUNT in the UPDATE part. Use VALUES(aggregate_column) instead:
INSERT INTO table_aggregate
(id_aggregate, aggregate_column)
(SELECT id_detail, COUNT(DISTINCT(detail_column))
FROM table_detail
GROUP BY id_detail)
ON DUPLICATE KEY UPDATE
aggregate_column = VALUES(aggregate_column);
http://rextester.com/KTEDM89215
You can put the aggregation inside a subquery. Then you can refer to the computed value in the update
INSERT INTO
table_aggregate (id_aggregate, aggregate_column)
SELECT
*
FROM
(
SELECT
id_detail,
COUNT(DISTINCT(detail_column)) AS count
FROM
table_detail
GROUP BY
id_detail
) AS aggr
ON DUPLICATE KEY
UPDATE
aggregate_column = aggr.count
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'
)
I have a subquery as follows : which will select the id's according to the condition first and delete
the records ,
Delete from post_master_user_map WHERE id IN
(SELECT id FROM `post_master_user_map` WHERE posted_by_user_id=110);
But it gives me the following error :
You can't specify target table 'post_master_user_map' for update in FROM clause
What is wrong with this ? thanks in advance .
UPDATE
This also fails , I dont why
DELETE FROM `post_master_user_map` WHERE `reshare_id` in (SELECT id FROM `post_master_user_map` WHERE
posted_by_user_id=110);
This error occurs when you try to modify a table and select from the same table in sub query.
Anyway to solve that error change your query as follows
Delete from post_master_user_map WHERE posted_by_user_id=110;
For the updated query(in your question) use following
DELETE t1 FROM post_master_user_map as t1 INNER JOIN
post_master_user_map as t2 ON t1.reshare_id=t2.id and t2.posted_by_user_id=110
By this MySQL DELETE FROM with subquery as condition : MySQL does not allow the table you're deleting from be used in a subquery for the condition.
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.