Target table not Updatable error - mysql

I need to run this query :
UPDATE (
SELECT r.*
FROM booked r
INNER JOIN (
SELECT a.st_code as from_t
, b.st_code as to_t
FROM `stops_at` a
CROSS JOIN `stops_at` b
WHERE (a.stop_no < b.stop_no)
and (a.train_no = b.train_no)
and (a.train_no = '11280')
) new
ON (r.st_from = new.from_t)
and (r.st_to = new.to_t)
and r.date = '2013-04-16'
) temp
SET temp.seat_ac = temp.seat_ac-5
but on execution it gives an error:
#1288-The target table temp of the UPDATE is not updatable.
Any solutions?

I think your UPDATE syntax is incorrect. See if this works:
UPDATE booked r
INNER JOIN (
SELECT a.st_code as from_t
, b.st_code as to_t
FROM `stops_at` a
CROSS JOIN `stops_at` b
WHERE (a.stop_no < b.stop_no)
and (a.train_no = b.train_no)
and (a.train_no = '11280')
) new
ON r.st_from = new.from_t
and r.st_to = new.to_t
and r.date = '2013-04-16'
SET r.seat_ac = r.seat_ac-5

Related

Error near Group and order by while converting ORACLE query to Aurora DB MYSQL query

iam trying to convert following ORACLE query into Aurora DB MYSQL query.
Changed few oracle functions to mysql functions.
SELECT MIN(TP.TP_ID) TP_ID,
TC_GRP.TC_GRP_NB TC_GRP_NB,
MIN(TC.TC_ID) TC_ID,
MIN(TC.FAC_ID) FAC_ID,
LISTAGG(TC_GST.TXN_IDVL_PTY_ID, ',') WITHIN GROUP (ORDER BY TC_GST.TXN_IDVL_PTY_ID) AS TXN_IDVL_PTY_ID
FROM RES_MGMT.TP
INNER JOIN RES_MGMT.TPS
ON TPS.TP_ID = TP.TP_ID
INNER JOIN RES_MGMT.TC_GRP
ON TC_GRP.TPS_ID = TPS.TPS_ID
INNER JOIN RES_MGMT.TC
ON (TC.TC_GRP_NB = TC_GRP.TC_GRP_NB AND
TC.TC_TYP_NM = 'AccommodationComponent' AND
TC.TRVL_STS_NM = 'Booked' AND TC.UPGRD_TC_ID IS NULL)
INNER JOIN RES_MGMT.TC_GST
ON TC_GST.TC_ID = TC.TC_ID
WHERE TC_GRP.ADV_INTERNT_CHKIN_IN = 'Y'
AND TPS.SRC_ACCT_CTR_ID = 2
AND TC.FAC_ID in (80010383)
AND TRUNC(TC.TC_STRT_DTS) = TO_DATE('20210930', 'yyyyMMdd')
AND EXISTS (SELECT *
FROM RES_MGMT.TC TC1
WHERE TC1.TC_GRP_NB = TC.TC_GRP_NB
AND TC1.TC_TYP_NM = 'AdmissionComponent'
AND TC1.TRVL_STS_NM = 'Booked')
GROUP BY TC_GRP.TC_GRP_NB;
MySQLQuery has modified as follows:
SELECT MIN(TP.TP_ID) TP_ID,
TC_GRP.TC_GRP_NB TC_GRP_NB,
MIN(TC.TC_ID) TC_ID,
MIN(TC.FAC_ID) FAC_ID,
LISTAGG(TC_GST.TXN_IDVL_PTY_ID, ',') WITHIN GROUP (ORDER BY TC_GST.TXN_IDVL_PTY_ID) AS TXN_IDVL_PTY_ID
FROM RES_MGMT.TP
INNER JOIN RES_MGMT.TPS
ON TPS.TP_ID = TP.TP_ID
INNER JOIN RES_MGMT.TC_GRP
ON TC_GRP.TPS_ID = TPS.TPS_ID
INNER JOIN RES_MGMT.TC
ON (TC.TC_GRP_NB = TC_GRP.TC_GRP_NB AND
TC.TC_TYP_NM = 'AccommodationComponent' AND
TC.TRVL_STS_NM = 'Booked' AND TC.UPGRD_TC_ID IS NULL)
INNER JOIN RES_MGMT.TC_GST
ON TC_GST.TC_ID = TC.TC_ID
WHERE TC_GRP.ADV_INTERNT_CHKIN_IN = 'Y'
AND TPS.SRC_ACCT_CTR_ID = 2
AND TC.FAC_ID in (80010385)
AND DATE(TC.TC_STRT_DTS) = STR_TO_DATE('20210930', '%Y-%m-%d')
AND EXISTS (SELECT *
FROM RES_MGMT.TC TC1
WHERE TC1.TC_GRP_NB = TC.TC_GRP_NB
AND TC1.TC_TYP_NM = 'AdmissionComponent'
AND TC1.TRVL_STS_NM = 'Booked')
GROUP BY TC_GRP.TC_GRP_NB;
Iam getting mysql syntax error near WITHIN GROUP.
Kindly suggest what need to be changed.

Unknown column in 'IN/ALL/ANY subquery'

Why am I getting this error? Error is showing up in the OR clause, but I'm selecting gl_ID inside the SELECT statement.
; DECLARE v_firstDate DATETIME
SET #p_ID = 368
SELECT SUBDATE(NOW(), 1) INTO v_firstDate;
SELECT t_ID, t_firstName, t_lastName
FROM t_table
WHERE
((#p_ID IN (
SELECT tt_ID
FROM tt_tableTwo
INNER JOIN st_stats ON (st_ID = tt_ID)
INNER JOIN da_data ON (da_ID = st_ID AND da_name IN ("allCompanies", "allglobals"))
)
))
OR
(
(gl_ID IN ( //problem is here
SELECT gl_ID
FROM gl_globals
INNER JOIN tr_transport ON (tr_id = gl_caseID AND tr_idOther = #p_ID)
INNER JOIN co_countries ON (co_ID = gl_ID AND co_ID = #p_ID)
)
)
)
Error message:
Unknown column 'gl_ID' in 'IN/ALL/ANY subquery'
Should I be using an AS or a HAVING?
gl_ID is not accessible in the OR clause because it doesn't exist in t_table. An inner join under the first FROM clause solves the problem.
; DECLARE v_firstDate DATETIME
SET #p_ID = 368
SELECT SUBDATE(NOW(), 1) INTO v_firstDate;
SELECT t_ID, t_firstName, t_lastName
FROM t_table
INNER JOIN gl_globals ON (t_ID = gl_ID) // fix
WHERE
((#p_ID IN (
SELECT tt_ID
FROM tt_tableTwo
INNER JOIN st_stats ON (st_ID = tt_ID)
INNER JOIN da_data ON (da_ID = st_ID AND da_name IN ("allCompanies", "allglobals"))
)
))
OR
(
(gl_ID IN (
SELECT gl_ID
FROM gl_globals
INNER JOIN tr_transport ON (tr_id = gl_caseID AND tr_idOther = #p_ID)
INNER JOIN co_countries ON (co_ID = gl_ID AND co_ID = #p_ID)
)
)
)

how to solve MYSQL ERROR 1248 (42000): Every derived table must have its own alias [duplicate]

I need to run this query :
UPDATE (
SELECT r.*
FROM booked r
INNER JOIN (
SELECT a.st_code as from_t
, b.st_code as to_t
FROM `stops_at` a
CROSS JOIN `stops_at` b
WHERE (a.stop_no < b.stop_no)
and (a.train_no = b.train_no)
and (a.train_no = '11280')
) new
ON (r.st_from = new.from_t)
and (r.st_to = new.to_t)
and r.date = '2013-04-16'
) temp
SET temp.seat_ac = temp.seat_ac-5
but on execution it gives an error:
#1288-The target table temp of the UPDATE is not updatable.
Any solutions?
I think your UPDATE syntax is incorrect. See if this works:
UPDATE booked r
INNER JOIN (
SELECT a.st_code as from_t
, b.st_code as to_t
FROM `stops_at` a
CROSS JOIN `stops_at` b
WHERE (a.stop_no < b.stop_no)
and (a.train_no = b.train_no)
and (a.train_no = '11280')
) new
ON r.st_from = new.from_t
and r.st_to = new.to_t
and r.date = '2013-04-16'
SET r.seat_ac = r.seat_ac-5

MYSQL update a column with a subtract in a inner join

I want to update column 'pagadas' from 'remisiones' with a calculated value from another 2 tables. My problem is I´m not be able to write the subtract into the inner join.
I´ve got this:
UPDATE remisiones AS r
INNER JOIN
(
SELECT remi, SUM(cantidad*precio) AS 'total'
FROM detalleremi
GROUP BY remi
)
AS d
ON r.id = d.remi
SET pagadas = 's'
WHERE d.total = 250000
This works too:
UPDATE remisiones AS r
INNER JOIN
(
SELECT remisionId, coalesce(SUM(cantidadp), 0) 'pagos'
FROM pagos
GROUP BY remisionId
)
AS d
ON r.id = d.remisionId
SET pagadas = 's'
WHERE d.pagos = 250000
But how can I subtract total - pagos ?
SELECT remi, SUM(cantidad*precio)
FROM detalleremi
GROUP BY remi -
SELECT remisionId, coalesce(SUM(cantidadp), 0)
FROM pagos
GROUP BY remisionId AS deuda
and set as:
SET pagadas = 's'
WHERE x.deuda = 0
Is this what you want?
UPDATE remisiones r INNER JOIN
(SELECT remi, SUM(cantidad*precio) as total
FROM detalleremi
GROUP BY remi
) d
ON r.id = d.remi INNER JOIN
(SELECT remisionId, coalesce(SUM(cantidadp), 0) as pagos
FROM pagos
GROUP BY remisionId
) p
ON r.id = p.remisionId
SET pagadas = 's'
WHERE d.total = p.pagos

Commands out of sync; you can't run this now

I'm doing a query on mysql with a subquery in the SELECT clause and I got this error: #2014 - Commands out of sync; you can't run this command now
I haven't been able to solve it. Can you help me please?
SELECT e.IdDocumento, e.CveDistrito, d.STCT_NOM, e.CveJuzgado, j.CTJU_DESCR, e.NumDocumento, e.IdRamoDocumento, r.Descripcion....., (SELECT pd.nombre FROM ParteDocumento AS pd WHERE pd.IdDocumento = e.IdDocumento) AS NombreActor
FROM Documentos AS e
INNER JOIN CtRamo AS r ON ( r.IdRamo = e.IdRamoDocumento )
INNER JOIN CtEstado AS edo ON ( edo.CveEstado = e.CveEstado )
INNER JOIN CTDISJUD AS d ON (d.STCT_NUM = e.CveDistrito)
INNER JOIN CTJUZGAD AS j ON (j.CTJU_MUNIC = e.CveDistrito AND j.CTJU_JUZGA = e.CveJuzgado)
LEFT JOIN CtMedioPresentacion AS m ON ( m.IdMedioPresentacion = e.IdMedioPresentacion )
WHERE e.IdTipoDocumento =1 AND e.EsRecibido =1
This is my query. I did the same query in SQL Server and it works!
I am not 100% sure, but i think you can not use table from from clause in a subselect inside of select. But why not simply join the table instead of subselect:
SELECT e.IdDocumento, e.CveDistrito, d.STCT_NOM, e.CveJuzgado, j.CTJU_DESCR, e.NumDocumento, e.IdRamoDocumento, r.Descripcion....., pd.nombreNombreActor
FROM Documentos AS e
INNER JOIN CtRamo AS r ON ( r.IdRamo = e.IdRamoDocumento )
INNER JOIN CtEstado AS edo ON ( edo.CveEstado = e.CveEstado )
INNER JOIN CTDISJUD AS d ON (d.STCT_NUM = e.CveDistrito)
INNER JOIN CTJUZGAD AS j ON (j.CTJU_MUNIC = e.CveDistrito AND j.CTJU_JUZGA = e.CveJuzgado)
LEFT JOIN CtMedioPresentacion AS m ON ( m.IdMedioPresentacion = e.IdMedioPresentacion )
LEFT JOIN ParteDocumento as pd on pd.IdDocumento = e.IdDocumento
WHERE e.IdTipoDocumento =1 AND e.EsRecibido =1