Return data from VIEWS using Knex.js - mysql

How can I get data from views in my mysql database using knex.js ?
I want to return this data in my nodejs route.get
SELECT c.id, nome_1
FROM pessoas_juridicas as pj
INNER JOIN pessoas as p
ON p.id = pj.pessoa_id
INNER JOIN clientes as c
ON c.pessoa_id = pj.pessoa_id
UNION
SELECT c.id, nome_1
FROM pessoas_fisicas as pf
INNER JOIN pessoas as p
ON p.id = pf.pessoa_id
INNER JOIN clientes as c
ON c.pessoa_id = pf.pessoa_id
WHERE p.id = 1
;

let sql = knex.from('live_calls' + ' as r')
.select('r.uuid', 'r.destination', 'r.current_status', 'r.src')
.where('r.uuid', '=', uuid );
sql.then((response) => {
console.log(response);
});
Here live_calls is a MySQL View.

Related

FIND_IN_SET is getting wrong results

First, I searched about the same problem but I didn't find an appropriate solution.
My problem is with the following code, it's returning wrong results:
SELECT FbID,FhID,
FbRef,
FbDate,
(D.AccName) AS DName,
FbQuan,
CONCAT(CategoryName,'-',ProductName) AS ProdName,
(C.AccName) AS CusName,
FhPurPrice,FbSalePrice,
(R.AccName) AS ResoName,
Curr1.CurrencyName,
Curr2.CurrencyName,
Plc1.PlaceName AS FhResoPlaceName,
Plc2.PlaceName AS FbCusPlaceName,
'linked' AS xLinkStatus,
1 AS xStatus
FROM tblfatora2 F2
INNER JOIN tblfatora1 F1 ON F1.FhRef = F2.FhRef
INNER JOIN tblproducts P ON P.ProductID = F1.FhProduct
INNER JOIN tblcategories CT ON CT.CategoryID = P.ProductCategory
INNER JOIN tblaccounts R ON R.AccID = F1.FhReso
INNER JOIN tblaccounts C ON C.AccID = F2.FbCus
INNER JOIN tblaccounts D ON D.AccID = F1.FhDriver
INNER JOIN tblcurrencies Curr1 ON C.AccCurrID = Curr1.CurrencyID
INNER JOIN tblcurrencies Curr2 ON R.AccCurrID = Curr2.CurrencyID
LEFT JOIN tblplaces Plc1 ON F1.FhResoPlace = Plc1.PlaceID
LEFT JOIN tblplaces Plc2 ON F2.FbCusPlace = Plc2.PlaceID
WHERE FIND_IN_SET(`FhID`, '18313,18314')
ORDER BY FbDate, FbID
the results that it gives me are: enter image description here
note: I use FIND_IN_SET here because I can't use (IN) where I use that SQL statement in a procedure inside vb.net code:
Public Sub MySql_GetLinked()
xDtAll = New DataTable()
Dim xPar(0) As MySqlParameter
xPar(0) = New MySqlParameter("#FhID", MySqlDbType.String) With {
.Value = LinkedFatora}
xClsMySql.GetData(xSqlLinked, xDtAll, xPar)
End Sub
So I fill the variable (LinkedFatora) by loop and I use the same SQL statement but I replace (WHERE FIND_IN_SET('FhID', '18313,18314')) with (WHERE FIND_IN_SET('FhID', #FhID)).
I looked for the error's reason but couldn't catch it.
I found the problem, it's changing (WHERE FIND_IN_SET(FhID, '18313,18314')) to (WHERE FIND_IN_SET(FbID, '18313,18314')):
SELECT FbID,FhID,
FbRef,
FbDate,
(D.AccName) AS DName,
FbQuan,
CONCAT(CategoryName,'-',ProductName) AS ProdName,
(C.AccName) AS CusName,
FhPurPrice,FbSalePrice,
(R.AccName) AS ResoName,
Curr1.CurrencyName,
Curr2.CurrencyName,
Plc1.PlaceName AS FhResoPlaceName,
Plc2.PlaceName AS FbCusPlaceName,
'linked' AS xLinkStatus,
1 AS xStatus
FROM tblfatora2 F2
INNER JOIN tblfatora1 F1 ON F1.FhRef = F2.FhRef
INNER JOIN tblproducts P ON P.ProductID = F1.FhProduct
INNER JOIN tblcategories CT ON CT.CategoryID = P.ProductCategory
INNER JOIN tblaccounts R ON R.AccID = F1.FhReso
INNER JOIN tblaccounts C ON C.AccID = F2.FbCus
INNER JOIN tblaccounts D ON D.AccID = F1.FhDriver
INNER JOIN tblcurrencies Curr1 ON C.AccCurrID = Curr1.CurrencyID
INNER JOIN tblcurrencies Curr2 ON R.AccCurrID = Curr2.CurrencyID
LEFT JOIN tblplaces Plc1 ON F1.FhResoPlace = Plc1.PlaceID
LEFT JOIN tblplaces Plc2 ON F2.FbCusPlace = Plc2.PlaceID
WHERE FIND_IN_SET(`FbID`, '18313,18314')
ORDER BY FbDate, FbID

how to join 10 table by using inner joins and group by condition

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

mysql where not in to left outer join

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;

Wrong sql statement?

I have a select:
SELECT
oa.einheit,
op.id, op.artikelnummer,
op.preis_pro_einheit, op.p1_einheit, op.p2_einheit, op.p3_einheit, op.zusatztext, op.position,
oa.artikel, oa.beschreibung, oa.einheit, oa.anzahl_parameter, oa.su_gewerk, oa.mbs_artikel,
oa.mindermenge_kleiner_als, oa.zulage_mindermengen_artikel_nummer,
ae.p1_einheit AS p1_einheit_description,
ae.p2_einheit AS p2_einheit_description,
ae.p3_einheit AS p3_einheit_description
FROM
objekt_position op
INNER JOIN objekt_artikel oa
ON oa.artikelnummer = op.artikelnummer
AND oa.id_objekt = op.id_objekt
AND oa.id_subunternehmer = op.id_subunternehmer
INNER JOIN artikel_einheit ae
ON ae.bezeichnung = oa.einheit
INNER JOIN subunternehmer s
ON s.subunternehmernummer = op.id_subunternehmer
INNER JOIN subunternehmer_user su
ON su.id_subunternehmer = s.subunternehmernummer
WHERE
su.id_user = 1
AND
op.id_objekt = 1486598
AND
oa.artikelnummer = 172
GROUP BY op.id
ORDER BY
op.position
but it seems that my inner join objekt_artikel are not working. what should i change? because i didnt get a result. But there are results in the table.

slow query with joins

Please am having difficulty in optimizing this query. What am trying to achieve is to join about 8 tables, of which only about 3 of the tables contains large data (1.5m records). This query returns expected records but is taking 1min to run which is bad.
I know it can be optimized to perform far better, pls i need assistance from you experts. I have index on the fields used for join already.
SELECT topic_id,
topic_title,
unit_name_abbrev,
sch_name_abbrev,
picture_small_url AS thumbnail,
profile_pix_upload_path,
first_name,
last_name,
topic_poster,
topic_replies,
topic_views,
topic_last_post_time AS topic_post_time,
sch_sub_forum_id
FROM (_sch_forum_topics
INNER JOIN _users
ON ( _users.userid = _sch_forum_topics.topic_poster )
INNER JOIN _profile
ON _profile.userid = _users.userid
INNER JOIN _class
ON _users.classid = _class.classid
INNER JOIN _level
ON _class.level_id = _level.id
INNER JOIN _unit
ON _class.unitid = _unit.unitid
INNER JOIN _department
ON _unit.deptid = _department.deptid
INNER JOIN _faculty
ON _department.facid = _faculty.facid
INNER JOIN _university
ON _faculty.schid = _university.schid)
WHERE _sch_forum_topics.sch_sub_forum_id = 4
ORDER BY _sch_forum_topics.topic_last_post_time DESC
LIMIT 0, 15
Try to filter before making JOIN's.
SELECT topic_id,
topic_title,
unit_name_abbrev,
sch_name_abbrev,
picture_small_url AS thumbnail,
profile_pix_upload_path,
first_name,
last_name,
topic_poster,
topic_replies,
topic_views,
topic_last_post_time AS topic_post_time,
sch_sub_forum_id
FROM
( select * FROM sch_forum_topics WHERE sch_sub_forum_id = 4
ORDER BY topic_last_post_time DESC
LIMIT 0, 15 ) main
INNER JOIN _users
ON ( _users.userid = main.topic_poster )
INNER JOIN _profile
ON _profile.userid = _users.userid
INNER JOIN _class
ON _users.classid = _class.classid
INNER JOIN _level
ON _class.level_id = _level.id
INNER JOIN _unit
ON _class.unitid = _unit.unitid
INNER JOIN _department
ON _unit.deptid = _department.deptid
INNER JOIN _faculty
ON _department.facid = _faculty.facid
INNER JOIN _university
ON _faculty.schid = _university.schid);