What's wrong with my MySQL statement? - mysql

UPDATE table1 SET announcer = ( SELECT memberid
FROM ( table1
JOIN users ON table2.username = table1.announcer
) AS a
WHERE a.username = table1.announcer )
#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 'a where
a.username=table1.announcer)'
at line 1

Try:
UPDATE announcements a
SET announcer =
(SELECT memberid
FROM users u
WHERE u.username = a.announcer)

You can also do the JOIN in the UPDATE
UPDATE announcements JOIN users
SET announcements.announcer=users.memberid
WHERE announcements.username=users.username;
Note: For safty reasons (until your sure announcers get copied over right) I'd instead create a new column, say announcerNew then
UPDATE announcements JOIN users
SET announcements.announcerNew=users.memberid
WHERE announcements.username=users.username;

Related

mysql update with several joins

I would like to update with 2 join.... but :
UPDATE glpi.glpi_users
FROM
glpi.glpi_groups_users
INNER JOIN glpi.glpi_groups ON glpi.glpi_groups_users.groups_id = glpi.glpi_groups.id
INNER JOIN glpi.glpi_users ON glpi.glpi_users.id = glpi.glpi_groups_users.users_id
SET glpi.glpi_users.id = 2
WHERE
glpi.glpi_groups.`name` LIKE 'technique'
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM
glpi.glpi_groups_users
INNER JOIN glpi.glpi_groups ON glpi.glpi_groups_us' at line 2
Time: 0s
thanks for your help
I think that the syntax you want is:
UPDATE glpi.glpi_users u
INNER JOIN glpi.glpi_groups_users gu ON gu.users_id = u.id
INNER JOIN glpi.glpi_groups g ON g.id = gu.groups_id
SET u.id = 2
WHERE g.name = 'technique'
That is: MySQL update/join syntax doesn't have a FROM clause - it looks like UPDATE ... JOIN ... SET ... WHERE ....
Notes:
since no wildcards do appear in the right side operand, name LIKE 'technique' is equivalent to name = 'technique'
table aliases make the query easier to write and read

sql update with inner join and where

UPDATE newsreactions
SET newsreactions.enabled = '0'
FROM newsreactions
INNER JOIN users ON newsreactions.memberId = users.id
WHERE users.active = '0' AND users.comment LIKE '%spam%'
For some reason I'm getting a syntax 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 newsreactions INNER JOIN users ON newsreactions.memberId = users.id WHERE u' at line 3
Can't figure it out though.
If I replace the update and set by a select it works fine.
Error 1064 is a MySQL syntax error. The correct MySQL syntax is:
UPDATE newsreactions nr INNER JOIN
users u
ON nr.memberId = u.id
SET nr.enabled = 0
WHERE u.active = 0 AND u.comment LIKE '%spam%';
Notes:
The JOIN goes in the UPDATE clause.
Table aliases makes the query easier to write and to read.
I am guessing that enabled and active are really numeric values. If so, do not use single quotes.
The join clause should come before the set clause, and there should be no from clause in MySQL:
UPDATE newsreactions
JOIN users ON newsreactions.memberId = users.id
SET newsreactions.enabled = '0'
WHERE users.active = '0' AND users.comment LIKE '%spam%'

SQL: SUM and Update query error,

I'm trying to update my riders total ucipoints by following an example, with some small modifications, but I just get an error from it.
example
UPDATE P
SET extrasPrice = t.TotalPrice
FROM BookingPitches AS P INNER JOIN
(
SELECT
PitchID,
SUM(Price) TotalPrice
FROM
BookingPitchExtras
GROUP BY PitchID
) t
ON t.PitchID = p.ID
I got the code from this answer:
SQL Update to the SUM of its joined values
My code looks like this:
UPDATE P
SET ucipoeng = t.TotalPoints
FROM rytterlagsesong AS P INNER JOIN
(
SELECT
rytterid,
SUM(poeng) AS TotalPoints
FROM
t_ucipoeng
WHERE year(dato)='2016'
GROUP BY rytterid
) t
ON t.rytterid = P.rytterid AND t.sesong='2016'
I get the error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM rytterlagsesong AS P INNER JOIN ( SELECT rytterid, SUM(ucip' at line 3
Can someone help me find the error?
DB Structure:
rytterlagsesong: rytterid - ucipoeng - sesong
t_ucipoeng: rytterid - dato - poeng
So I want to sum the points (poeng) of all races in 2016 (dato=date) for a rider
And update that riders totalpoint (ucipoeng) for this season (sesong)
The update / join syntax is different per database. For mysql, use this:
UPDATE rytterlagsesong r
INNER JOIN (
SELECT rytterid, SUM(poeng) AS TotalPoints
FROM t_ucipoeng
WHERE year(dato)='2016'
GROUP BY rytterid
) t ON t.rytterid = r.rytterid AND t.sesong='2016'
SET r.ucipoeng = t.TotalPoints

SQL : Update table with select

I have a query like this
UPDATE t_prd_cost_compare
SET
2015_AUG_PRD_UNIT_PRICE=i.PRD_UNIT_PRICE,
2015_AUG_PRD_SELLING_PRICE=i.PRD_SELLING_PRICE,
2015_AUG_PRD_IN_PATIENT_LIST_PRICE=i.PRD_IN_PATIENT_LIST_PRICE,
2015_AUG_PRD_OUT_PATIENT_LIST_PRICE=i.PRD_OUT_PATIENT_LIST_PRICE
FROM (
SELECT PRODUCTID,PRD_UNIT_PRICE,PRD_SELLING_PRICE,PRD_IN_PATIENT_LIST_PRICE,PRD_OUT_PATIENT_LIST_PRICE
FROM t_product_catalog
LEFT JOIN T_adjust ON IAJ_PRODUCTID=PRODUCTID AND IAJ_ADJNO IS NULL
WHERE PRODUCTID>1 AND (DATE(IAJ_DATE) = '2015-01-01')
GROUP BY IAJ_PRODUCTID
) AS i
WHERE i.PRODUCTID = t_prd_cost_compare.PRODUCTID
I get error like this
Error Code: 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 (
SELECT PRODUCTID,PRD_UNIT_PRICE,PRD_SELLING_PRICE,PRD_IN_PATIENT_LIST_PRI' at line 7
I done checked the select statement is correct, but I still get error!
Any idea?
Issue solved, here is the solution
Update
Competition as C
inner join (
select CompetitionId, count(*) as NumberOfTeams
from PicksPoints as p
where UserCompetitionID is not NULL
group by CompetitionID
) as A on C.CompetitionID = A.CompetitionID
set C.NumberOfTeams = A.NumberOfTeams
refer from: mysql update query with sub query

SQL Update a table using a view

I have the following SQL code:
SELECT
`table1`.`field1`
, `view1`.`newfield1`
FROM
`table1`
INNER JOIN `GCOTDA2`.`view1`
ON (`table1`.`id1` = `view1`.`id1`) AND (`table1`.`id2` = `view1`.`id2`);
The query works fine but now I want to copy the view1.newfield1 to table1.field1. Thus, I wrote the following statement:
UPDATE `table1`
SET
`table1`.`field1` = `view1`.`newfield1`
FROM
`table1`
INNER JOIN `view1`
ON (`table1`.`id1` = `view1`.`id1`) AND (`table1`.`id2` = `view1`.`id2`);
However, the update does not work and I got this error message:
Error Code: 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
`table1`
INNER JOIN `view1`
' at line 4
I checked on google and other questions in this site such as: How do I UPDATE from a SELECT in SQL Server? and Error Code: 1064 in mysql with no luck. (MySQL Server 5.5.27)
I need someone to illuminate me, thanks!
Try this on for size:
UPDATE 'table1` t JOIN `view1` v
ON t.id1 = v.id1 AND t.id2 = v.id2
SET t.field1 = v.newfield1
You can do this with a subquery:
update
table1
set
table1.field1 = (select view1.newfield1 from view1 where view1.id1 = table1.id1 and view1.id2 = table1.id2)
where
exists (select null from view1 where view1.id1 = table1.id1 and view1.id2 = table1.id2)
http://sqlfiddle.com/#!2/64774/1/0