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
Related
my query is like below
SELECT
*
FROM`punit_master` `a`
LEFT JOIN
(
SELECT *
FROM
(
SELECT *
FROM _map_contacts as t
LEFT JOIN contact_master as c on c.contact_id = t.contact_id
where t.punit_id =a.punit_id
limit 1
) tm LEFT JOIN contactdetail_master cd ON cd.contactdetail_id = tm.contactdetail_id
) b ON b.agreement_id = a.curr_tenant_agreement_id
it gives me error of a.punit_id is undefine
how can I use mail table value in left join inside sub query
SELECT
*
FROM `punit_master` `a`
LEFT JOIN
(
SELECT *
FROM
(
SELECT *
FROM _map_contacts as t
LEFT JOIN contact_master as c
ON c.contact_id = t.contact_id
) tm
LEFT JOIN contactdetail_master cd
ON cd.contactdetail_id = tm.contactdetail_id
) b
ON b.agreement_id = a.curr_tenant_agreement_id and a.punit_id = b.punit_id
Try this! :)
i want to convert this query into joins , this is a task which i got from my manger that Please convert the query below into Join format.
SELECT
capder.cder_id,
capman.cman_name AS Manufacturer,
caprange.cran_name AS Range1,
capmod.cmod_name AS Model,
capmod.cmod_code AS ModelCode,
capder.cder_doors AS Doors,
nvdbodystyle.bs_description AS BodyStyle,
captrim.ctrim_name AS Trim1,
capder.cder_name as Trim2,
capder.cder_drivetrain AS DriveTrain,
nvdmodelyear.MY_ref AS DerivativeModelYear,
group_concat(distinct(nvddictonaryoption.DO_Description) order by nvddictonaryoption.DO_Description SEPARATOR '|') AS ExteriorColour
FROM capman,
caprange,
capmod,
nvdbodystyle,
capder,
nvdmodelyear,
captrim,
nvdoptions,
nvddictonaryoption,
nvddictionarycategory
WHERE capman.cman_code = caprange.cran_mantextcode
AND caprange.cran_code = capmod.cmod_rancode
AND nvdbodystyle.bc_code = capmod.cmod_bodystyle
AND capmod.cmod_code = capder.cder_modcode
AND capder.cder_id = nvdmodelyear.my_id
AND captrim.ctrim_code = capder.cder_trimcode
AND nvdoptions.OPT_Id = capder.cder_id
AND nvdoptions.opt_optioncode = nvddictonaryoption.DO_OptionCode
AND nvddictionarycategory.dc_catcode = nvddictonaryoption.do_catcode
AND nvddictionarycategory.dc_cth_type = 'C'
AND SUBSTR(nvdmodelyear.MY_ref,1,4) > 2006
group by cder_id,SUBSTR(nvdmodelyear.MY_ref,1,4),captrim.ctrim_name
MySQL workbench 6.3 CE.
i tried this way
SELECT
cd.cder_id,
cm.cman_name AS Manufacturer,
cr.cran_name AS Range1,
cmd.cmod_name AS Model,
cmd.cmod_code AS ModelCode,
cd.cder_doors AS Doors,
nb.bs_description AS BodyStyle,
ct.ctrim_name AS Trim1,
cd.cder_name as Trim2,
cd.cder_drivetrain AS DriveTrain,
nmy.MY_ref AS DerivativeModelYear,
group_concat(distinct(ndo.DO_Description) order by ndo.DO_Description SEPARATOR '|') AS ExteriorColour
FROM capman cm
inner join caprange cr on cm.cman_code = cr.cran_mantextcode
inner join capmod cmd on cr.cran_code = cmd.cmod_rancode
inner join nvdbodystyle nb on nb.bc_code = cmd.cmod_bodystyle
inner join capder cd on cmd.cmod_code = cd.cder_modcode
inner join nvdmodelyear nmy on cd.cder_id = nmy.my_id
inner join captrim ct on ct.ctrim_code = cd.cder_trimcode
inner join nvdoptions nop on nop.OPT_Id = cd.cder_id
inner join nvddictonaryoption ndo on nop.opt_optioncode = ndo.DO_OptionCode
inner join nvddictionarycategory ndc on ndc.dc_catcode = ndo.do_catcode AND ndc.dc_cth_type = 'C' AND SUBSTR(nmy.MY_ref,1,4) > 2006
group by cder_id,SUBSTR(nmy.MY_ref,1,4),ct.ctrim_name;
am i did correct? it is giving correct result but it is taking too long to execute and get results. is there any better way to execute this query only with joins syntax and i don't have access to put index to the table
From these tables I have written this subquery and its giving results as per requirements.
Needs expert guidence to improve this query or if we can also be able to use join for these tables.
Query:
select ps,st from pac where con in (select
config from config where logi in
( select id from logicalnode where physi
in (select id from ysicalnode where mas =11)));
SELECT
payloadstr
,starttime
FROM packetdb.packet
INNER JOIN packetdb.configuration
ON packetdb.packet.configid = packetdb.configuration.idconfig
INNER JOIN packetdb.logicalnode
ON packetdb.configuration.idconfig = packetdb.logicalnode.id
INNER JOIN packetdb.physicalnode
ON packetdb.logicalnode.physicalnodeid = packetdb.physicalnode.id and packetdb.physicalnode.macaddress=117769729
You could try using exists:
select payloadstr,starttime from packetdb.packet p
where exists(select 1 from packetdb.configuration c
where p.configid = id
and exists(select 1 from packetdb.logicalnode l
where c.logicalnodeid = id
and exists(select 1 from packetdb.physicalnode
where macaddress = 117769729
and l.physicalnodeid = id)
Using Left join
select payloadstr,starttime
from packet
left join Configuration on Configuration.IDconfig = packet.configID
left join logicalnode on logicalnode.ID = Configuration.logicalnodeid
left join physicalnode on physicalnode.ID = logicalnode.physicalnodeid
where macaddress =117769729
You can try below - using JOIN
select payloadstr,starttime
from packetdb.packet a inner join packetdb.configuration b on a.configid=b.idconfig
inner join packetdb.logicalnode c on logicalnodeid=c.id
inner join packetdb.physicalnode d on physicalnodeid=d.id
where macaddress =117769729
Try this
select pa,sta
from
pack p
INNER JOIN
confi c
ON
p.confi = c.idco
INNER JOIN
logice l
ON
c.logic = l.id
INNER JOIN
physiode pn
ON
l.physicalnodeid = pn.id
WHERE macaddress =123
I have the following query and would like to convert it to using a left outer join instead of a not in to see if it would run faster that way. It's currently taking this query about 40 seconds to run on our database. I'm not familiar enough with using outer joins for this type of thing to convert it myself.
select
c.contact_id as contact_id,
c.orgid as organization_id,
c.first_name as first_name,
c.last_name as last_name,
a.address_state as state
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
where a.address_state = 'OH'
and (c.orgid = 45 or c.orgid = 55)
and c.contact_id NOT IN (
select pc.contact_id
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
inner join cnx_contact_group_participant as gp on c.contact_id = gp.contact_id
inner join cnx_contact_participant_role as cr on gp.participant_role_uid = cr.participant_role_uid
inner join cnx_contact_group as cg on gp.group_uid = cg.group_uid
inner join cnx_contact_group_participant as pgp on cg.primary_participant_uid = pgp.participant_uid
inner join cnx_contact as pc on pgp.contact_id = pc.contact_id
where (c.orgid = 45 or c.orgid = 55)
and cr.name = 'Applicant'
);
select
c.columns
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
LEFT JOIN
(Subquery goes here) x
ON x.contact _id = c.contact_id
where a.participant_state = 'OH'
and c.orgid IN(45,55)
and x.contact_id IS NULL;
I've got the query below that's pulling data from a number of tables to create an update:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
It works fine but when there are plenty of records in papr_down then it takes ages to process. Any ideas about how it can be optimized?
What I think is the join between the emailAddress is the issue here, you can try out with the join with the Id's.
If you provide us the screen shot of the below query ::
EXPLAIN Select * from
en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
WHERE sd.fieldid = 33
As I know we should use joins only for the columns which are preset in SELECT clause and for other joins we should implement using WHERE clause
Please try following query:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls
on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down AS pd1
INNER JOIN email.papr_data AS pd2 on pd1.paper_id = pd2.id
WHERE
email.papr_exam.id in (select exam from email.papr_data where exam = 1)
AND
email.papr_levl.id in (select level from email.papr_data where level = 4 )
AND
email.papr_subj.id in (select subject from email.papr_data)
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
I can not execute this at my machine since i don't have the schema