MySql - Update with Select - mysql

I need to update a table with a select inside. This is my query so far:
Update T_STATO_CASA
Set UTENTE = 'Admin'
Where ID_CASA in (
Select ID
From T_CASA
Where ID_RICHIESTA
In (437869, 437233, 437235, 437876)
)
But it returns the following error: "Subquery returned more than 1 value. This is not permitted when the subquery follows
=, !=, <, <= , >, >=
or when the subquery is used as an expression."
The subquery returns exactly 4 results if I run it separately. Is my syntax wrong? Thanks.
EDIT: all the suggested solutions that are using JOIN give me syntax error, as if MySql expects only the update-set-where command sequence. For instance I cannot write something like
update T_STATO_CASA as x
set [...]
where [...]
because it gives me syntax error: "Incorrect syntax near word AS. Expected SET"

UPDATE t_stato_casa x
JOIN t_casa y
ON y.id = x.id_casa
SET x.utente = 'admin'
WHERE y.id_richiesta IN(437869, 437233, 437235, 437876)

The reason is that your subquery will return more than one row of record
The more suitable way is to use JOIN instead
UPDATE T_STATO_CASA
JOIN T_CASA t
ON t.id = ID_CASA
AND t.ID_RICHIESTA
IN (437869, 437233, 437235, 437876)
SET UTENTE = 'Admin
If you still want to use subquery,you need to use group_concat to make the result into one record

This was the right syntax for my case:
UPDATE t_stato_casa
SET
UTENTE = 'Admin'
FROM
t_stato_casa AS sc
INNER JOIN T_CASA AS c
ON c.ID = sc.ID_CASA
WHERE
c.ID_RICHIESTA in (437869, 437233, 437235, 437876)
Thanks to everyone.

Related

MySQL: Error Code: 1111. Invalid use of group function

Please help me, what's wrong with my query. I need to update land_superseded_assessment table from land_propertyassessment from different database.
UPDATE erptax_db.land_superseded_assessment lsa
INNER JOIN erptax_dumalinao.land_propertyassessment lp
ON lp.landinfo_arpno = lsa.old_arp_no
SET
lsa.old_assessed_value=(SUM(lp.landpropertyassessment_assessmentvalue))
Try like this
UPDATE erptax_db.land_superseded_assessment lsa
INNER JOIN (
SELECT landinfo_arpno, SUM(landpropertyassessment_assessmentvalue) as total
FROM erptax_dumalinao.land_propertyassessment
GROUP BY landinfo_arpno
) lp ON lsa.old_arp_no = lp.landinfo_arpno
SET
lsa.old_assessed_value=lp.total

SQL Incorrect syntax near the keyword 'order'

I am writing in SQL and trying to update a new colunm "rankFeild" in table "seker_w_adress". The updating is just fine, but when I am trying to order the rows selected so the colunm will get the biggest value exist for this ID, I am getting this error:
Incorrect syntax near the keyword 'order'.
Here is my code:
update seker_w_adress set rankFeild=si
from seker_w_adress as swa
join
(
select ID,gender,age,seker_ind as si
from last_seker
)ls2
on swa.ID=ls2.ID and ((ls2.gender='female' and ls2.age<47) or (ls2.gender='male' and ls2.age<45))
order by ls2.si desc;
I tried to put "order by" into the ls2 brackets, like that:
update seker_w_adress set rankFeild=si
from seker_w_adress as swa
join
(
select ID,gender,age,seker_ind as si
from last_seker
order by ls2.si desc
)ls2
on swa.ID=ls2.ID and ((ls2.gender='female' and ls2.age<47) or (ls2.gender='male' and ls2.age<45))
;
but it produces another error. What's the problem?
I found the answer - thanks to Akina:
update seker_w_adress set rankFeild=si
from seker_w_adress as swa
join
(
select ID,max(seker_ind) as si
from last_seker
group by ID
)ls2
on swa.ID=ls2.ID
join last_seker as ls
on ls.ID=ls2.ID and ((ls.gender='female' and ls.age<47) or (ls.gender='male' and ls.age<45));

One table update accoriding to another table MySQL update error

I have two tables name activties and post_media now I want to update the media background color in activities table according to post media table record but when I run query it give me error.
Query
UPDATE A
SET A.bg_color = M.bg_color
FROM activities A
INNER JOIN post_media M ON A.relation_id = M.user_post_id AND A.media=M.file
WHERE A.relation_id>0
Error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'FROM activities A INNER JOIN post_media M ON A.relation_id =
M.user_post_' at line 3
UPDATE syntax is different from SELECT. There is no FROM clause usage in UPDATE statement.
General flow is: UPDATE <table name> [JOIN <other tables>] SET ...
UPDATE activities A
INNER JOIN post_media M ON A.relation_id = M.user_post_id AND A.media=M.file
SET A.bg_color = M.bg_color
WHERE A.relation_id>0
Check documentation here for full syntax and further understanding: https://dev.mysql.com/doc/refman/8.0/en/update.html
Update query with use of join is different than SELECT query. Here you need to add tables before SET clause and all conditions in WHERE clause like SELECT.
e.g/
UPDATE t1, t2
SET t1.field = t2.field
WHERE condition 1
AND condition 2
So your query will be like as below:
UPDATE activities A, post_media M
SET A.bg_color = M.bg_color
WHERE A.relation_id = M.user_post_id
AND A.media=M.file
AND A.relation_id>0
Try this one.

SQL Update using values from other columns in same table

I have an SQL table, "data", with the following columns:
id (int),
date1 (datetime),
date2 (datetime),
difference (float)
What I would like to do is update this table so that the "difference" column contains the difference in years (as a float) between date2 and date1, only when date2 doesn't equal '0000-00-00 00:00:00'
I was thinking of something along the lines
Update data m
SET difference = datediff("hh",m.date2, m0.date1)/8765 --there are 8765 hours per year so this should give me my float
FROM data m0
WHERE m.id = m0.id
AND m.date2 <> '0000-00-00 00:00:00';
Yet when I try this I get an error stating "SQL syntax error. Check your MySQL server version for the right syntax to use near data SET difference = datediff("hh,m.date2,m0.date10/8765) from 'd' at line 1"
How would I modify my sql statement to get the desired results?
EDIT:
I am using xampp's phpMyAdmin interface to run this sql statement
Update statements are suppose to be on the form:
UPDATE table
SET column1 = expression1,
column2 = expression2,
...
WHERE conditions;
Your from clause is thus irrelevant.
You can use joins and sub select-queries though to link to other tables.
-- There are excellent demos on how to achieve that (actually, a complete duplicate):
UPDATE TABLEA a
JOIN TABLEB b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
I figured it out, thanks to Norbert's advice
I used
update data m, data m0
set m.difference = datediff(date(m.date2),date(m0.date2))/365
where m.id = m0.id
and m.date2 <> '0000-00-00 00:00:00';
I was using the incorrect syntax for mysql datediff, which takes 2 dates as a parameter and returns the difference in days.
Try this syntax for update with multiple tables:
Update data m, data m0
SET m.difference = datediff("hh",m.date2, m0.date1)/8765
WHERE m.id = m0.id
AND m.date2 <> '0000-00-00 00:00:00';
Note that in the SET statement you also need to indicate which table you are updating else it will still give you a nice warning.

Join on Join mysql

I am getting an SQL ERROR (1064) Syntax. Is what i am trying to do allowed? As i don't see the syntax error.
`SELECT isc_products.prodname, isc_product_variations.* , isc_product_variation_combinations.vcoptionids,
FROM isc_products
JOIN isc_product_variations
ON isc_products.prodvariationid = isc_product_variations.variationid
JOIN isc_product_variation_combinations
ON isc_product_variation_combinations.vcvariationid = isc_product_variations.variationid`
You have isc_product_variations.variationid twice in your ON statements. Check, if this is what you want, or if there is a second key in you perhaps need isc_product_variations
You have an error on the first line. You have a comma that shouldn't be there:
SELECT isc_products.prodname,
isc_product_variations.* ,
isc_product_variation_combinations.vcoptionids,
-- ^
FROM ...
I'd also advise you not to use SELECT isc_product_variations.* but instead list the columns you want explicitly.