MySQL Join clause in Update - mysql

When trying to perform this query:
>mysql_query("UPDATE contracts
SET x = '1'
FROM contracts
INNER JOIN employees
ON contracts.contract_employeeid=employees.employee_id
WHERE experience >= '6'") or die(mysql_error());
I get the following error message:
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 contracts INNER JOIN employees ON contracts.contract_employeeid=employees.employee_id WHERE experience >= '6'
In words, I need to set x=1 on the table "contracts" for the employees who have more than 6 years of experience (to do so, I need to join with the table "employees" on employee_id=contract_employeeid since their experience is stored in that table)

You can probably move the JOIN part of your query before the SET statements:
UPDATE contracts
INNER JOIN employees ON contracts.contract_employeeid = employees.employee_id
SET x = '1'
WHERE experience >= '6'

UPDATE contracts, employees
SET contracts.x = '1'
WHERE contracts.contract_employeeid=employees.employee_id
AND employees.experience >= '6'

Related

SQL Syntax error when using cases inside a SELECT

I am trying to have an if else condition in this select statement. I am either passing a customerID or a accountID into this mapper and thus I need to check which one is being passed. I had the first case where there was no customerId (thus accountId is passed), and so the inner join is performed. The second one is when there is no accountID (thus customerId was passed). Then at the end of the cases, I want to order it by descending dates.
SELECT i.invoice_id, i.account_id, total_amount_due, due_date, bp.eff_date, bp.end_date, total_amount_due
(
CASE
WHEN customerId = null
THEN INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id
WHERE account_id=#{accountId}
ELSE INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id
INNER JOIN server.cust_acct_rel car ON car.account_id = i.account_id
WHERE car.customer_id=#{customerId}
END)
ORDER BY end_date DESC
However, when I run this, I get this error.
org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: java.sql.SQLSyntaxErrorException: 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 'INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id WHERE ' at line 5
Does anyone know what I am doing wrong here?
use IFNULL
select ...,..., IFNULL(customerID,accountID) newid
from
table1
left outer join table2
order by date desc

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 with Join statement

I have two tables : first one stores main informations and the second stores some extra informations. I need to update the first table with the help of some data stored at second table.
My SELECT statement is working
SELECT news.news_id,
news.title,
news.cat_id,
news.sub_cat_id,
news_extra.date_vision_tr
FROM news_extra
JOIN news
ON news.news_id = news_extra.news_id
WHERE news.cat_id=1 AND sub_cat_id=5 AND news_extra.date_vision_tr < CURDATE()
Than I tried UPDATE statement like this
UPDATE news SET news.sub_cat_id=8
FROM news
INNER JOIN news_extra
ON news.news_id = news_extra.news_id
WHERE news.cat_id=1 AND sub_cat_id=5 AND news_extra.date_vision_tr < CURDATE()
But its not working. Giving this error
*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 news INNER JOIN news_extra ON news.news_id =
news_extra.news_id WHERE news.' at line 2*
The syntax you are using is for SQL Server.
Here is the syntax for MySQL
UPDATE news a
INNER JOIN news_extra b
ON a.news_id = b.news_id
SET a.sub_cat_id = 8
WHERE a.cat_id = 1 and
sub_cat_id = 5 and
b.date_vision_tr < CURDATE()
You must be familiar with SQL Server more. The syntax you are using will work in SQLServer. For MySQL:
UPDATE news
INNER JOIN news_extra
ON news.news_id = news_extra.news_id
SET news.sub_cat_id=8
WHERE news.cat_id=1 and sub_cat_id=5 and news_extra.date_vision_tr < CURDATE()
--Try this way.
Check similar thread

What's wrong with my MySQL statement?

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;