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

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

Related

MySQL: how to UPDATE query based on SELECT Query

I have following SQL query, that does a bulk update on MySQL server:
UPDATE res_booking AS rbg
SET rbg.host_id = (
SELECT vw.entity_id FROM res_booking AS rb
INNER JOIN pass_single as ps ON (ps.book_id = rb.booking_id)
INNER JOIN voucher_who as vw ON (vw.pass_id = ps.pass_id)
WHERE rb.booking_id = rbg.booking_id
)
WHERE rbg.host_id IS NULL;
Executing it, gives me following error:
Error Code: 1093. You can't specify target table 'rbg' for update in FROM clause
How can be this query be rewritten, so it will work?
I would write this as an update join:
UPDATE res_booking AS rbg
INNER JOIN res_booking AS rb
ON rb.booking_id = rbg.booking_id
INNER JOIN pass_single AS ps
ON ps.book_id = rb.booking_id
INNER JOIN voucher_who AS vw
ON vw.pass_id = ps.pass_id
SET
rbg.host_id = vw.entity_id
WHERE
rbg.host_id IS NULL;
Technically the self join on res_booking should be a left join, because your current update would always assign to every record, regardless of whether or not the subquery returns a value. However, I suspect that inner join logic is what you would really want here.
You can use a correlated subquery. Just leave res_booking out of the subquery:
UPDATE res_booking rbg
SET rbg.host_id = (SELECT vw.entity_id
FROM pass_single ps JOIN
voucher_who vw
ON vw.pass_id = ps.pass_id
WHERE ps.book_id = rbg.booking_id
)
WHERE rbg.host_id IS NULL;
MySQL generates an error if you re-use the table being updated in a subquery. However, it is not needed. Just fix the correlation clause to use the ps table.
Note that this could return an error if the subquery returns more than one row. That is a good thing. If it happens, you will need to figure out how to handle this situation.

MySql - Update with Select

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.

SQL How to Update by INNER JOIN -

Please help me figure this out since I tried everything from this forum but still haven't found a solution.
Well, I have two tables:
prices
manufacturers
I want to change the values of two fields that are both in table prices.
And I will just give specific values to those.
The fields are:
prices.override (in which I want to give the value 0) and
prices.product_discount_id (in which I want to give the value 66)
BUT I want to change the fields ONLY FOR the manufacturer with ID 31.
So, I first check that an INNER JOIN works fine.
SELECT manufacturers.manufacturer_id,
prices.product_id,
prices.product_price,
prices.override,
prices.product_discount_id
FROM manufacturers
INNER prices
ON manufacturers.product_id=prices.product_id
AND manufacturers.manufacturer_id=31;
But when I try to update the two fields, I do not know how to make that work.
For example, I tried this but it didn't work:
UPDATE prices
SET prices.override=1
FROM
INNER JOIN prices
ON manufacturers.product_id=prices.product_id
AND manufacturers.manufacturer_id=31;
I also tried this:
UPDATE prices
SET prices.override=1,
INNER JOIN manufacturers
ON prices.virtuemart_product_id = manufacturers.virtuemart_product_id
AND manufacturers.manufacturer_id=31;
What did i do wrong? Usually the error message I get is:
#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 jos_virtuemart_product_prices prices INNER JOIN jos_virtuemart_product_man' at line 3
I read something for alias but still no result.
Any help would be appreciated!
You have a few syntax problems , MySQL UPDATE..JOIN syntax is :
UPDATE T
JOIN t2 ON()
SET ..
WHERE ..
Secondly, you had an unnecessary comma after the SET prices.override=1, so:
UPDATE prices
INNER JOIN manufacturers
ON prices.virtuemart_product_id = manufacturers.virtuemart_product_id
AND manufacturers.manufacturer_id=31
SET prices.override=1
Try this if you are using SQL Server for mysql above is fine:
UPDATE p SET p.override=1
FROM prices p
INNER JOIN manufacturers ON
p.virtuemart_product_id =manufacturers.virtuemart_product_id
AND manufacturers.manufacturer_id=31;
This is the correct syntax in MySQL:
UPDATE prices p JOIN
manufacturers m
USING (product_id)
SET p.override=1
WHERE m.manufacturer_id = 31;
Note the use of table aliases. These make a query easier to write and to read.
The syntax that you are using is appropriate for SQL Server.
In MySQL the UPDATE with JOIN syntax is different, the SET operator should comes after the JOIN statement.
The correct UPDATE query is
UPDATE prices P
INNER JOIN manufacturers M ON P.virtuemart_product_id = M.virtuemart_product_id
AND M.manufacturer_id = 31;
SET P.override = 1;
UPDATE prices
SET prices.override=1
FROM manufacturers
INNER JOIN prices
ON manufacturers.product_id=prices.product_id
AND manufacturers.manufacturer_id=31;

Inner join with of 3 tables

I have three tables which I am trying to join using the sample in this question. However I am getting below error.
Error:
#1054 - Unknown column 'referrer.Referrer_Name' in 'field list'
Code:
SELECT pupils.Pupils_Surname,
pupils.Pupils_FirstName,
referrer.Referrer_Name,
progress_track.test_1,
progress_track.test_2,
progress_track.test_3,
progress_track.test_4,
progress_track.test_5,
progress_track.test_6,
progress_track.test_7,
progress_track.test_8,
progress_track.test_9,
progress_track.test_10
FROM pupils
INNER JOIN progress_track ON progress_track.Progress_Report_Pupils_ID=pupils.Pupils_ID;
INNER JOIN referrer ON pupils.Pupils_Referrer, referrer.Referrer_ID ;
First let me highlight the faults in your query:
1) The query ended unexpectedly for a typo
2) The last INNER JOIN contained syntax error.
I've highlighted the faults in the image given below:
Remedy:
SELECT
pupils.Pupils_Surname,
pupils.Pupils_FirstName,
referrer.Referrer_Name,
progress_track.test_1,
progress_track.test_2,
progress_track.test_3,
progress_track.test_4,
progress_track.test_5,
progress_track.test_6,
progress_track.test_7,
progress_track.test_8,
progress_track.test_9,
progress_track.test_10
FROM
pupils
INNER JOIN progress_track ON progress_track.Progress_Report_Pupils_ID = pupils.Pupils_ID
INNER JOIN referrer ON pupils.Pupils_Referrer = referrer.Referrer_ID;
In response to your comment:
Could you please post this as an answer. It worked but it still did
not show any data from the referrer table
If the join condition doesn't meet then there's no way you can see
data unless you use LEFT JOIN. Please check your data first.
Please ensure that you have data which meets these two conditions :
progress_track.Progress_Report_Pupils_ID = pupils.Pupils_ID
referrer.Referrer_ID = pupils.Pupils_Referrer
Please remove the semicolon at the end of the first inner join statement. Because, the query is tried to terminate the statement after the first semicolon of the query itself. So, this is the reason for the column Referrer_Name is not referring the table:"referrer".
original:
FROM pupils
INNER JOIN progress_track ON progress_track.Progress_Report_Pupils_ID=pupils.Pupils_ID;
INNER JOIN referrer ON pupils.Pupils_Referrer, referrer.Referrer_ID ;
Use this:
FROM pupils
INNER JOIN progress_track ON progress_track.Progress_Report_Pupils_ID=pupils.Pupils_ID
INNER JOIN referrer ON pupils.Pupils_Referrer = referrer.Referrer_ID ;

MySql update syntax error from inner join

UPDATE
`universities`
SET
`universities`.countryid = `countries`.id,
FROM
`universities`
INNER JOIN
`countries`
ON
`universities`.country = `countries`.name
When I try to run the sql statements above via PhpMyAdmin, it would give syntax errors. I wrote the statements based on this answer.
This is the correct syntax in MySQL:
UPDATE universities u JOIN
countries c
ON u.country = c.name
SET u.countryid = c.id;
In addition, I introduced table aliases (so the query is easier to write and to read) and removed an extraneous comma.