Update Table with DISTINCT SELECTed values from same TABLE - mysql

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.

Related

mySQL UPDATE WHERE with subquery gives error

I want to update a table with a subquery and always get an error.
Now i made a very simplified version (which makes not much sense but shows my error)
UPDATE a_test SET categoryID = '2956' WHERE id IN (
(
SELECT id from a_test
)
)
This ends in this error:
#1093 - Table 's_articles_categories' is specified twice, both as a target for 'UPDATE' and as a separate source for data
Why do i get this error?
When i use aliasses for the table a_test i get the same error.
This is the full query i want to use with the same error:
UPDATE s_articles_categories SET categoryID = '2956' WHERE id IN
(
SELECT s_articles_categories.id FROM `s_articles`
LEFT JOIN s_articles_categories ON s_articles.id = s_articles_categories.articleID
WHERE s_articles_categories.categoryID NOT IN (
SELECT id FROM s_categories
WHERE s_categories.id NOT IN (SELECT parent FROM s_categories WHERE parent IS NOT null GROUP BY parent)
)
)
One solution to the simplified query is to wrap the subquery inside another subquery:
UPDATE a_test
SET categoryID = '2956'
WHERE id IN (SELECT id FROM (SELECT id FROM a_test) x );
This trick forces MySQL to materialize the subquery on a_test, so that the values coming from the subquery aliased as x are not affected as the update proceeds.

Sql Select a minimum value from a table column and insert the results in another table column in one SQL statement

I am trying to get a minimum value from the Candidate table and insert that value in the MinTotal table. Can you do both in one SQL statement?
Here's my SQL Statement:
UPDATE MinTotal SET MinTotal.min_total= MIN(CandidateID.TotalVotes);
UPDATE MinTotal a
INNER JOIN (SELECT MIN(c.TotalVotes) min_vote, c.CandidateID FROM Candidate c
GROUP BY c.CandidateID) b ON b.CandidateID = a.CandidateID
SET a.min_total = b.min_vote;
Try the above. This is specific for each candidate, else you can use the other answers provided.
You have to use a select so you can properly set your MIN().
One way of doing that would be like that:
UPDATE MinTotal
SET
min_total = Cmin.minresult
FROM (
SELECT MIN(TotalVotes) as minresult
from CandidateID
) Cmin
In general, that would be one way to solve the Problem. In this case you would set the minresult for every row you have in your MinTotal table. If you dont want that, you may need to be more specific about your desired output and add some examples in your question
UPDATE MinTotal
SET MinTotal.min_total = (
SELECT MIN(TotalVotes)
FROM CandidateID
);

Mysql error 1093 - Can't specify target table for update in FROM clause - update column in same table

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
);

subquery in where clause of UPDATE statement

I have the database of ATM card in which there are fields account_no,card_no,is_blocked,is_activated,issue_date
Fields account number and card numbers are not unique as old card will be expired and marked as is_block=Y and another record with same card number ,account number will be inserted into new row with is_blocked=N . Now i need to update is_blocked/is_activated with help of issue_date i.e
UPDATE card_info set is_blocked='Y' where card_no='6396163270002509'
AND opening_date=(SELECT MAX(opening_date) FROM card_info WHERE card_no='6396163270002509')
but is doesn't allow me to do so
it throws following error
1093 - You can't specify target table 'card_info' for update in FROM clause
Try this instead:
UPDATE card_info ci
INNER JOIN
(
SELECT card_no, MAX(opening_date) MaxOpeningDate
FROM card_info
GROUP BY card_no
) cm ON ci.card_no = cm.card_no AND ci.opening_date = cm.MaxOpeningDate
SET ci.is_blocked='Y'
WHERE ci.card_no = '6396163270002509'
That's one of those stupid limitations of the MySQL parser. The usual way to solve this is to use a JOIN query as Mahmoud has shown.
The (at least to me) surprising part is that it really seems a parser problem, not a problem of the engine itself because if you wrap the sub-select into a derived table, this does work:
UPDATE card_info
SET is_blocked='Y'
WHERE card_no = '6396163270002509'
AND opening_date = ( select max_date
from (
SELECT MAX(opening_date) as_max_date
FROM card_info
WHERE card_no='6396163270002509') t
)

strange mysql error

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;