What am i doing wrong with this update from select - mysql

I have been triying to make an update from a select the console keeps showing an 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
(
Select
a.id_activity as stat
, a.date_reg as l_clock
' at line 5
I would appreciate some help with it.
update user_stat set
clock_stat = a.stat
, datetime_lclock = a.l_clock
, date_fclock = a.f_clock
from
(
Select
a.id_activity as stat
, a.date_reg as l_clock
, date(c.date_reg) as f_clock
from log_activity a
inner join
(
select
max(id_reg) as last_Act,
min(id_reg) as first_Act
from log_activity
where uid = 1
)b on a.id_reg = b.last_Act
left join log_activity c on c.id_reg = b.first_Act
)a
where uid = 1;

You need to set value later, so the general syntax is some like :
UPDATE TABLEA a
JOIN TABLEB b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
update user_stat
join
(
Select
a.id_activity as stat
, a.date_reg as l_clock
, date(c.date_reg) as f_clock
from log_activity
) a
inner join
( select
max(id_reg) as last_Act,
min(id_reg) as first_Act
from log_activity where uid = 1
)b on a.id_reg = b.last_Act
left join log_activity c on c.id_reg = b.first_Act
set
clock_stat = a.stat
, datetime_lclock = a.l_clock
, date_fclock = a.f_clock

Related

UPDATE - SELECT - MYSQL #1093 - You can't specify target table 'temp1' for update in FROM clause

I can't find solution to correct this big query, I always receive an error from database.
I have tre tables and I need to make changes on some attribute on some condition:
UPDATE o36t_orders as temp1,
mytable as temp2
SET
temp1.bonifico = 1,
temp2.ultimo = 1
WHERE
temp1.id_order IN (
SELECT
id_order
FROM o36t_orders
LEFT JOIN o36t_address ON (o36t_address.id_address = o36t_orders.id_address_delivery)
LEFT JOIN mytable ON (
mytable.Causale = CONCAT(
o36t_address.lastname,
' ',
o36t_address.firstname
)
)
WHERE
o36t_orders.bonifico <> 1
)
AND temp2.id IN (
SELECT
id
FROM o36t_orders
LEFT JOIN o36t_address ON (o36t_address.id_address = o36t_orders.id_address_delivery)
LEFT JOIN mytable ON (
mytable.Causale = CONCAT(
o36t_address.lastname,
' ',
o36t_address.firstname
)
)
WHERE
o36t_orders.bonifico <> 1
)
Since the subqueries of the 2 IN clauses are identical (except the retuned column), I think that you can do what you want by a straight inner join of the 2 tables and that subquery (returning both columns):
UPDATE o36t_orders temp1
INNER JOIN (
SELECT
id, id_order
FROM o36t_orders
LEFT JOIN o36t_address ON (o36t_address.id_address = o36t_orders.id_address_delivery)
LEFT JOIN mytable ON (
mytable.Causale = CONCAT(
o36t_address.lastname,
' ',
o36t_address.firstname
)
)
WHERE
o36t_orders.bonifico <> 1
) t ON t.id_order = temp1.id_order
INNER JOIN mytable temp2 ON temp2.id = t.id
SET
temp1.bonifico = 1,
temp2.ultimo = 1

Unknown column in 'having clause' in mysql 5.5

i try to run a query with having:
SELECT
`doctors`.*,
(
SELECT GROUP_CONCAT(`areas`.`areaName` SEPARATOR ', ')
FROM `areas_has_doctors`
INNER JOIN `areas` ON `areas`.`areaId` = `areas_has_doctors`.`areaId`
WHERE `areas_has_doctors`.`doctorId` = `doctors`.`doctorId`
) as `areas`,
(
SELECT GROUP_CONCAT(`areas`.`areaId`)
FROM `areas_has_doctors`
INNER JOIN `areas` ON `areas`.`areaId` = `areas_has_doctors`.`areaId`
WHERE `areas_has_doctors`.`doctorId` = `doctors`.`doctorId`
) as `areasIdies`
FROM
`cats_has_doctors`
INNER JOIN `doctors` ON `doctors`.`doctorId` = `cats_has_doctors`.`doctorId`
WHERE
`cats_has_doctors`.`catId` = '1' && `doctors`.`disable` = 0
GROUP BY
`cats_has_doctors`.`relationId`
HAVING FIND_IN_SET('1,2,3,4', `areasIdies`)
Server with 10.0.21-MariaDB - MariaDB Server its working
but in server with:
5.5.61-cll - MySQL Community Server (GPL)
i got the error:
Unknown column 'areasIdies' in 'having clause'
what can i do ?
Maybe it thinks areasIdies is a variable?
I would try pulling the areasIdies subquery into a temp table & trying that.
Just keep the query as it is to test, but just do that at the top and change the find in set param.
CREATE TEMPORARY TABLE IF NOT EXISTS areasIdiesTemp AS (
SELECT GROUP_CONCAT(`areas`.`areaId`)
FROM `areas_has_doctors`
INNER JOIN `areas`
ON `areas`.`areaId` = `areas_has_doctors`.`areaId`
WHERE `areas_has_doctors`.`doctorId` = `doctors`.`doctorId`
);
SELECT
`doctors`.*,
(
SELECT GROUP_CONCAT(`areas`.`areaName` SEPARATOR ', ')
FROM `areas_has_doctors`
INNER JOIN `areas` ON `areas`.`areaId` = `areas_has_doctors`.`areaId`
WHERE `areas_has_doctors`.`doctorId` = `doctors`.`doctorId`
) as `areas`,
(
SELECT GROUP_CONCAT(`areas`.`areaId`)
FROM `areas_has_doctors`
INNER JOIN `areas` ON `areas`.`areaId` = `areas_has_doctors`.`areaId`
WHERE `areas_has_doctors`.`doctorId` = `doctors`.`doctorId`
) as `areasIdies`
FROM
`cats_has_doctors`
INNER JOIN `doctors` ON `doctors`.`doctorId` = `cats_has_doctors`.`doctorId`
WHERE
`cats_has_doctors`.`catId` = '1' && `doctors`.`disable` = 0
GROUP BY
`cats_has_doctors`.`relationId`
HAVING FIND_IN_SET('1,2,3,4', `areasIdiesTemp`);

Error when Multiple JOINS are used

I'm trying to execute the following query, Bu it gives a syntax error, Can some point me were the problem is?
select count(*) from (((
select * from testlink1915.TL_tcversions where execution_type = 2 and id = 66134) c
JOIN (select * from testlink1915.TL_nodes_heirachy) d
ON id = testlink1915.TL_nodes_heirachy.parent_id) a
JOIN (select * FROM testlink1915.TL_req_coverage where req_id = 67635) b
ON a.id = b.testcase_id);
Error appeared only when I added the following segment.
c JOIN (select * from testlink1915.TL_nodes_heirachy) d
ON id = testlink1915.TL_nodes_heirachy.parent_id
The error
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 'a JOIN (select * FROM testlink1915.TL_req_coverage where req_id = 67635) b ON a.' at line 1
Use:
c.id = d.parent_id
instead of
id = testlink1915.TL_nodes_heirachy.parent_id
Try this:
select count(*)
from (
select *
from (
select *
from (
select *
from testlink1915.TL_tcversions
where execution_type = 2
and id = 66134
) c join (
select *
from testlink1915.TL_nodes_heirachy
) d on c.id = d.parent_id
) a join (
select *
from testlink1915.TL_req_coverage
where req_id = 67635
) b on a.id = b.testcase_id
) t;
Please note that I use * everywhere. Replace it with the columns you need.
Please Try this
select count(1) from
(
select * from (
select * from testlink1915.TL_tcversions where execution_type = 2 and id = 66134 ) c
JOIN
(select * from testlink1915.TL_nodes_heirachy) d on a.id=d.parent_id
join
(select * FROM testlink1915.TL_req_coverage where req_id = 67635) a ON a.id = b.testcase_id
) as E join (select * from testlink1915.TL_nodes_heirachy) F ON E.id = F.parent_id
Try below query :
select count(*) from
(
(
select * from testlink1915.TL_tcversions
JOIN testlink1915.TL_nodes_heirachy ON id =
testlink1915.TL_nodes_heirachy.parent_id where execution_type = 2 and id
= 66134
) a
JOIN
(
select * FROM testlink1915.TL_req_coverage where req_id = 67635
) ON a.id = b.testcase_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

Join 3 table is giving an error

i have 3 tables.
tenant
tenant_id : int
category_id : int
category
category_id : int
category_name : varchar(50)
history
tenant_id : int
bulan_tahun : varchar(8)
counter : int
i want to join all of this table using this code:
SELECT a.tenant_id, a.category_name
FROM (
(tenant INNER JOIN category ON tenant.category_id = category.category_id) AS a
INNER JOIN (
SELECT tenant_id, counter FROM history WHERE
bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')
) AS b
on a.tenant_id = b.tenant_id
)
but this code produce an 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 'a INNER JOIN ( SELECT tenant_id, counter FROM history WHERE bulan_tahun ' at line 3
if i separate the sub select, it works perfectly
for the first select:
SELECT tenant_id, category_name
FROM
(tenant INNER JOIN category ON tenant.category_id = category.category_id)
and the second select:
SELECT tenant_id, counter FROM history WHERE
bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')
but if i join this together, the error occured
can anyone help me?
Problem lies here..
(tenant INNER JOIN category ON tenant.category_id = category.category_id)
Try restructuring your entire query like this
SELECT a.tenant_id, c.category_name
FROM
tenant t
INNER JOIN
category c
ON ( t.category_id = c.category_id )
INNER JOIN
history h
ON ( t.tenant_id = h.tenant_id )
WHERE
h.bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')