SQL query- Different databases with inner join - mysql

I have two DBs- RATINGSAPP and MIGRATIONDATA.
I want to update a table in RATINGSAPP with some values in a table in MIGRATIONDATA. I am trying to run this query:
update r set internal_id = m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
This gives me 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 ratingsapp.hotel03 as r inner join migrationdata.migration as m on r.hotel_' at line 1
But a similar select query works for me and gives proper results.
select r.hotel_id, m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
What I am doing wrong in the update query?

The correct MySQL syntax is:
update ratingsapp.hotel03 r inner join
migrationdata.migration as m
on r.hotel_id = m.restaurant_id
set internal_id = m.internal_id ;
There is no from clause in a MySQL update. You are using SQL Server/Postgres syntax.

Related

SQL UPDATE 2 table with JOIN

I want to update two tables at the same time, because the connection and update conditions are in the first table. And only simple information like the amount is in the second table
UPDATE
order,
order_product
SET
order.total = order.total*0.00001,
order_product.price = order_product.price*0.00001,
order_product.total = order_product.total*0.00001
FROM
order_product
LEFT JOIN
order ON order_product.order_id = order.order_id
WHERE
order.currency_code = "USD"
AND
order.currency_value = 0.00001000
I keep getting this error
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
order_product
LEFT JOIN
order ON order_id ...' at line 5
Your syntax is off for MySQL and looks to be taken from some other dialect. For a MySQL update join, use an explicit join as follows:
UPDATE order o
INNER JOIN order_product op
ON op.order_id = o.order_id
SET
o.total = o.total*0.00001,
op.price = op.price*0.00001,
op.total = op.total*0.00001
WHERE
o.currency_code = 'USD' AND
o.currency_value = 0.00001000;

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

DELETE query with Joins

I am running a query to delete from table.
delete from sps
inner join str on str.studentid = sps.studentid
where str.studentid like '%2012%psy%'
and str.semesterid=2
inner join papers on papers.id = sps.paperid
where papers.c_id=10
and papers.p_semid=2
I don't know why it is not showing any result and giving me an error.
The error 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
'inner join str on str.studentid=sps.studentid where str.studentid like '%2012%ps'
at line 2
Move the two predicates of the two WHERE clauses into one WHERE clause at the end of your query, as described by the Mysql DELETE syntax. Something like:
DELETE s
FROM sps s
INNER JOIN str ON str.studentid = s.studentid
INNER JOIN papers ON papers.id = s.paperid
WHERE str.studentid LIKE '%2012%psy%'
AND str.semesterid = 2
AND papers.c_id = 10
AND papers.p_semid = 2
You can use WHERE clause once in a single query.
Try this query
DELETE sps FROM sps
INNER JOIN str ON str.studentid=sps.studentid
INNER JOIN papers ON papers.id=sps.paperid 
WHERE papers.c_id=10 AND papers.p_semid=2
AND str.studentid LIKE '%2012%psy%' AND str.semesterid=2 
OR
DELETE FROM sps, str, papers
USING sps
INNER JOIN str ON str.studentid=sps.studentid
INNER JOIN papers ON papers.id=sps.paperid 
WHERE papers.c_id=10
AND papers.p_semid=2
AND str.studentid LIKE '%2012%psy%'
AND str.semesterid=2