update table when subquery matches - mysql

I am trying this query but it outputs a syntax error in the subquery.
What is the problem and how can be solved? thanks
UPDATE CompradorCategorias_new as A
SET A.idParent=(
SELECT idcategoria
FROM categoriasi18n_new
WHERE
(
SELECT SUBSTRING_INDEX(NomeComPath, '>', 2)
FROM CompradorCategorias_new
=
SELECT translationWithPath
FROM categoriasi18n_new
)
)

Everything looks good in your query until the WHERE clause - at that point, it's all kinds of wrong. You can actually drop that block and use a regular WHERE clause comparison (instead of a second sub-query):
UPDATE
CompradorCategorias_new AS A
SET
A.idParent = (
SELECT
idcategoria
FROM
categoriasi18n_new AS B
WHERE
B.translationWithPath = SUBSTRING_INDEX(A.NomeComPath, '>', 2)
)

Related

Error occur when try to delete via select #1241 - Operand should contain 1 column(s)

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

MySQL query to get fields from a table where the ID exists in another query

This must be fairly straight forward, as I tend to use ORMs I don't have to get my hands dirty often and am therefore struggling!
I have a database and want to get several fields from a table, that bit is easy..
SELECT main_table.registration_number, main_table.registered_name FROM main_table;
I want to filter the results based on another table, which is also easy..
SELECT second_table.registration_number FROM second_table WHERE this_field = '' AND that_field = '0';
Now the problem is I want to run the first query based on the second queries result set, I was thinking something like this:
SELECT main_table.registration_number, main_table.registered_name FROM main_table WHERE main_table.registration_number IN (SELECT * FROM second_table WHERE this_field = '' AND that_field = '0');
This gives me: Error Code: 1241. Operand should contain 1 column(s)
Am I handling this completely wrong?
Your subquery should do something like below,
(select * from table) in subquery is not what you really need to do your
so the subquery should return one column
(SELECT registration_number FROM second_table WHERE this_field = '' AND that_field = '0');
You cannot have multiple columns being returned in a subquery like
that, doing so it will result in such error
You have to select a column
SELECT main_table.registration_number, main_table.registered_name FROM
main_table WHERE main_table.registration_number IN (SELECT
registration_number FROM second_table WHERE this_field = '' AND
that_field = '0');

MYSQL query trim a string based on result of results of another query

I am trying to update a table ONLY if a condition of another table is met. These tables don't have really anything I can join on. (the phppos_app_config is just key/value pairs). The query below does NOT work as I am not allowed to do a count on a subquery.
Here is what I am trying to achieve (doesn't work as it is NOT allowed in sql; but just showing logic)
UPDATE phppos_giftcards SET giftcard_number = TRIM(LEADING ';' FROM giftcard_number)
WHERE
COUNT
(
SELECT value FROM phppos_app_config
WHERE `key` = 'disable_giftcard_detection' and `value` = '0'
) =1
I don't have mysql to test so just let me know if it does not work and I will delete
UPDATE phppos_giftcards SET giftcard_number = TRIM(LEADING ';' FROM giftcard_number)
WHERE ( SELECT count(*) FROM phppos_app_config
WHERE `key` = 'disable_giftcard_detection' and `value` = '0'
) = 1
The use of the Exists Clause in the subquery will simplify the sub query and will perform better as unnecessary comparisons will be avoided (assuming that you want to update if there us at least one record in the PHPOS_APP_CONFIG table with the specified key, value)
UPDATE phppos_giftcards SET giftcard_number = TRIM(LEADING ';' FROM giftcard_number)
WHERE exists
( SELECT count(*) FROM phppos_app_config
WHERE `key` = 'disable_giftcard_detection' and `value` = '0'
)

Update one table with values from another table using select statement

I want to update the ClaimNos for a few declarationNos in table1 according to table2.
UPDATE sg_report
SET ClaimNo = (
SELECT MAX( dummy.ClaimNo ) as ClaimNo
FROM dummy
WHERE ClaimNo REGEXP '^[0-9]+$' and sg_report.DeclarationNo = dummy.DeclarationNo
)
WHERE dummy.DeclarationNo = (
SELECT DISTINCT dummy.DeclarationNo
FROM dummy
WHERE dummy.DeclarationNo LIKE '30%'
)
This query doesnt give any errors but it doesnt work either. Whats wrong ?
First, I think you copied your query wrong. You have WHERE dummy.DeclarationNo and I suspect you actually have WHERE sg_report.DeclarationNo.
Paresh J is correct that your WHERE statement should include IN rather than an equal statement since it could return multiple rows.
But the answer to your question I suspect is related to the data type. I created a sample database and ran the following query and it worked just fine. I actually spent a while trying to debug it since it wasn't updating anything until I realized that there was nothing to update!
UPDATE sg_report
SET ClaimNo = (
SELECT MAX( dummy.ClaimNo ) as ClaimNo
FROM dummy
WHERE ClaimNo REGEXP '^[0-9]+$' AND sg_report.DeclarationNo = dummy.DeclarationNo
)
WHERE sg_report.DeclarationNo IN (
SELECT DISTINCT dummy.DeclarationNo
FROM dummy
WHERE dummy.DeclarationNo LIKE '2%'
)

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;