i have 2 queries:
UPDATE dws_photogallery_albums a
SET a.photoscount=(
SELECT COUNT(*) FROM dws_photogallery_photos p
WHERE p.albumid=a.albumid)
UPDATE dws_photoportfolio_photos a
SET a.photoscount=(
SELECT COUNT(*) FROM dws_photoportfolio_photos p
WHERE p.albumid=a.albumid)
first works ok, but second gives me error:
#1093 - You can't specify target table 'a' for update in FROM clause
Tables are identical (differs only by name).
what can it be?
UPD: Men, i'm so sorry, it's just my missprint, queries must be like that:
UPDATE dws_photogallery_albums a
SET a.photoscount=(
SELECT COUNT(*) FROM dws_photogallery_photos p
WHERE p.albumid=a.albumid)
UPDATE dws_photoportfolio_albums a
SET a.photoscount=(
SELECT COUNT(*) FROM dws_photoportfolio_photos p
WHERE p.albumid=a.albumid)
And they both works ok for me.
Thanks for answers, need more coffee
It means, that you can't update the table you are reading from. Aliases won't solve the problem. It could lead to inconsistencies. You have to work with temporary tables or in your case with variables.
you are updating same table that you use in nested select.
you can't do this:
update table X
where ... ( Select ... from X )
It is not strange You can't specify target table for update in FROM clause.
Notice that you are having the same table for update and select in second query
Try this query -
UPDATE
dws_photoportfolio_photos a
JOIN (
SELECT albumid, COUNT(*) cnt FROM dws_photoportfolio_photos GROUP BY albumid
) p
ON p.albumid = a.albumid
SET a.photoscount = p.cnt;
Related
I`m trying to delete a lot of data via select. This select work appropriate and returns in result 75k+ rows. I need to delete them, but when I try to delete it this error occurs
#1241 - Operand should contain 1 column(s). I'm using PHPMyAdmin.
DELETE FROM `crm_wsal_metadata`
WHERE `occurrence_id` = ANY
(SELECT *
FROM `crm_wsal_metadata`
WHERE `name` = `PostDate` AND `value` BETWEEN str_to_date('2018-12-26', '%Y-%m-%d') AND str_to_date('2020-05-31', '%Y-%m-%d')
GROUP BY `occurrence_id`)
Use
... SELECT `occurence_id` ...
instead of SELECT *. The group by clause forces you to use only grouped columns and aggregations, not star (perhaps unless some proprietary quirks I don't recommend to rely on).
I had found the answer and will try to write it step by step:
Why does this error happen?
In MySQL, you can't modify the same table which you use in the SELECT part.
This behavior is documented at http://dev.mysql.com/doc/refman/5.6/en/update.html
How to make such thing happen?
There are two ways:
Join the table to itself
UPDATE tbl AS a
INNER JOIN tbl AS b ON ....
SET a.col = b.col
Nest the subquery deeper into a from clause
UPDATE tbl SET col = (
SELECT ... FROM (SELECT.... FROM) AS x);
Personally, in my case the code looked like this:
DELETE FROM crm_wsal_metadata
WHERE occurrence_id = ANY (
SELECT occurrence_id FROM (
SELECT occurrence_id FROM crm_wsal_metadata WHERE name = "PostDate" AND value BETWEEN str_to_date('2018-12-26', '%Y-%m-%d') AND str_to_date('2020-05-31', '%Y-%m-%d') AS search) )
Sorry for such bad styling. Im new with it :)
I have this table
What I want to do is that Select the attr_id WHERE a DISTINCT name_en-GB appears and then SET that selected attr_id for that name_en-GB
I can do this by writing individual queries but I want to know is there any way I can do this in one query?
I have tried this
UPDATE sc_product_phrase
SET attr_id = (
SELECT DISTINCT
sc_product_phrase.caption,
sc_product_phrase.design_phrase_id
FROM
`sc_product_phrase` as x
GROUP BY
sc_product_phrase.caption
)
but this show error
[Err] 1093 - You can't specify target table 'sc_product_phrase' for
update in FROM clause
EDITED
I want my TABLE to look like the following http://sqlfiddle.com/#!2/d65eb/1
NOTE:- I dont want to use that query in that fiddle
Your SQL Fiddle makes the question much clearer. You want the minimum attribute id on all rows with the same value in the last column. You can do this with an update/join like this:
UPDATE table1 JOIN
(SELECT `name_en-GB`, min(Attr_Id) as minai
from table1
GROUP BY `name_en-GB`
) tt
on table1.`name_en-GB` = tt.`name_en-GB`
SET attr_id = tt.minai
WHERE table1.`name_en-GB` IN ('Bride Name', 'Child Grade') AND
table1.attr_id <> tt.minai;
I'm not sure if you need the in part. You can update all of them by removing that clause.
I know there are a lot of topics on this but I can't figure out how should I rewrite my query to make it work :(
Here my query. It's just should take currency rate from other table and calculate cost
update site_s_client_base_price
SET calculated_price_in_base_currency =
SELECT (site_s_currencies.rate * site_s_client_base_price.supplier_price) from
site_s_currencies, site_s_client_base_price
WHERE site_s_currencies.currency_id=site_s_client_base_price.currency_id
Please, help me with this
You can't seletc and update in the same table because it's locked, use the example above or use
Update TABLE1 t1 set FIELD1= ( select field1 from TABLE1 t2 where .....)
In your case you can fix this by fixing the subquery. You don't need to mention the outer table in the inner from clause. You want a correlated subquery:
update site_s_client_base_price bp
SET calculated_price_in_base_currency =
(SELECT c.rate * bp.supplier_price
FROM site_s_currencies c
WHERE c.currency_id = bp.currency_id
);
I was trying to execute this statement to delete records from the F30026 table that followed the rules listed.. I'm able to run a select * from and a select count(*) from with that statement, but when running it with a delete it doesn't like it.. it gets lost on the 'a' that is to define F30026 as table a
delete from CRPDTA.F30026 a
where exists (
select b.IMLITM from CRPDTA.F4101 b
where a.IELITM=b.IMLITM
and substring(b.IMGLPT,1,2) not in ('FG','IN','RM'));
Thanks!
This looks like an inner join to me, see MySQL - DELETE Syntax
delete a from CRPDTA.F30026 as a
inner join CRPDTA.F4101 as b on a.IELITM = b.IMLITM
where substring(b.IMGLPT, 1, 2) not in ('FG', 'IN', 'RM')
Please note the alias syntax as a and as b.
Instead of the 'exists' function, you can match the id (like you do in the where clause):
delete from CRPDTA.F30026 a
where a.IELITM IN (
select b.IMLITM from CRPDTA.F4101 b
where a.IELITM=b.IMLITM
and substring(b.IMGLPT,1,2) not in ('FG','IN','RM'));
I believe this is what you really want, all IELITMs which meet your criteria.
delete from CRPDTA.F30026
where IELITM IN (
select IMLITM from CRPDTA.F4101
where substring(IMGLPT,1,2) not in ('FG','IN','RM'));
I tried:
UPDATE giveaways SET winner = '1' WHERE ID = (SELECT MAX(ID) FROM giveaways)
But it gives:
#1093 - You can't specify target table 'giveaways' for update in FROM clause
This article seems relevant but I can't adapt it to my query. How can I get it to work?
Based on the information in the article you linked to this should work:
update giveaways set winner='1'
where Id = (select Id from (select max(Id) as id from giveaways) as t)
This is because your update could be cyclical... what if updating that record causes something to happen which made the WHERE condition FALSE? You know that isn't the case, but the engine doesn't. There also could be opposing locks on the table in the operation.
I would think you could do it like this (untested):
UPDATE
giveaways
SET
winner = '1'
ORDER BY
id DESC
LIMIT 1
Read more
update giveaways set winner=1
where Id = (select*from (select max(Id)from giveaways)as t)
create table GIVEAWAYS_NEW as(select*from giveaways);
update giveaways set winner=1
where Id=(select max(Id)from GIVEAWAYS_NEW);
Make use of TEMP TABLE:
as follows:
UPDATE TABLE_NAME SET TABLE_NAME.IsActive=TRUE
WHERE TABLE_NAME.Id IN (
SELECT Id
FROM TEMPDATA
);
CREATE TEMPORARY TABLE TEMPDATA
SELECT MAX(TABLE_NAME.Id) as Id
FROM TABLE_NAME
GROUP BY TABLE_NAME.IncidentId;
SELECT * FROM TEMPDATA;
DROP TABLE TEMPDATA;
You can create a view of the subquery first and update/delete selecting from the view instead..
Just remember to drop the view after.