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`);
Related
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.
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)
)
)
)
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
I have sql query:
SELECT leads.lead_id,
attributes.code AS attributeCode,
leads_notes.content AS noteContent,
leads_notes.task_type_id,
task_types.type_name
FROM leads,
leads_notes
INNER JOIN task_types
ON task_types.task_type_id = leads_notes.task_type_id
INNER JOIN leads_attributes
ON leads_attributes.lead_id = leads.lead_id
INNER JOIN attributes
ON attributes.attribute_id = leads_attributes.attribute_id
INNER JOIN leads_notes
ON leads_notes.lead_id = leads.lead_id
WHERE ( leads.ambassador = 1
OR leads.rents_bike = 1 )
AND ( leads.city <> '' )
AND ( leads.address <> ''
OR leads.address2 <> '' )
AND ( leads.country_id <> '' )
AND ( leads_attributes.attribute_id IN ( $attributes_id ) )
AND ( leads.lead_id = $lead_id )
But I get error:
Syntax error or access violation: 1066 Not unique table/alias:
'leads_notes''
How can I solve it? Thanks.
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
In your case you also had lead_notes twice in the FROM clause. It is easier to write and read the query if you use table aliases:
SELECT l.lead_id, a.code as attributeCode, ln.content as noteContent, ln.task_type_id, tt.type_name
FROM leads l JOIN
leads_notes ln
ON ln.lead_id = l.lead_id JOIN
task_types tt
ON tt.task_type_id = ln.task_type_id JOIN
leads_attributes la
ON la.lead_id = l.lead_id JOIN
attributes a
ON a.attribute_id = la.attribute_id
WHERE (l.ambassador=1 OR l.rents_bike=1) AND
(l.city <> '') AND
(l.address <>'' OR l.address2 <>'') AND
(l.country_id <> '') AND
(la.attribute_id IN ($attributes_id)) AND
(l.lead_id = $lead_id);
You have add leads_notes table double: one in after leads table and another in inner join, so remove one solve your problem like following
SELECT leads.lead_id,
attributes.code AS attributeCode,
leads_notes.content AS noteContent,
leads_notes.task_type_id,
task_types.type_name
FROM leads
INNER JOIN task_types
ON task_types.task_type_id = leads_notes.task_type_id
INNER JOIN leads_attributes
ON leads_attributes.lead_id = leads.lead_id
INNER JOIN attributes
ON attributes.attribute_id = leads_attributes.attribute_id
INNER JOIN leads_notes
ON leads_notes.lead_id = leads.lead_id
WHERE ( leads.ambassador = 1
OR leads.rents_bike = 1 )
AND ( leads.city <> '' )
AND ( leads.address <> ''
OR leads.address2 <> '' )
AND ( leads.country_id <> '' )
AND ( leads_attributes.attribute_id IN ( $attributes_id ) )
AND ( leads.lead_id = $lead_id )
I have following sql statement:
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (g.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN master_hankoya.goods g
ON ha.WEBHINID = g.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
I using INNER JOIN on multi-database ,so that you can see sc and master_hankoya is difference databases
When I run it ,I get error :
Unknown column 'g.goods_id ' in 'on clause'
You can see g alias for table master_hankoya.goods,and goods_id is a column in this
I guess that I have problem with INNER JOIN
Please help me correct it
Update : I check again and take a silly problem ,have a special character in query make it failed to run
try this
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (g.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN
(select * from master_hankoya.goods ) g
ON ha.WEBHINID = g.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
Don't use alias name for master_hankoya.goods like
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (master_hankoya.goods.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN master_hankoya.goods
ON ha.WEBHINID = master_hankoya.goods.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
INNER JOIN sc.HINMTA ha
Change this line to:
INNER JOIN HINMTA AS ha
INNER JOIN master_hankoya.goods g
Change that line to
INNER JOIN goods AS g
The syntax for setting an alias for a table is:
table_name AS table_alias