I have a question.
This is my query:
SELECT
branches.naam as branche_naam,
subbranches.naam as subbranche_naam,
specialiteiten.naam as specialiteiten_naam
FROM bedrijfgegevens
INNER JOIN subbranches on subbranches.id = company.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
WHERE bedrijfgegevens.id in
(SELECT
bedrijfgegevens.id FROM bedrijfgegevens
INNER JOIN subbranches on subbranches.id = bedrijfgegevens.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
WHERE branches.naam = "test"
AND subbranches.naam = "demo"
AND specialiteiten.naam = "bla")
I think it's way to long and I am using the same tables.
What it does is:
subquery: get the company id where "specialiteiten.naam" = "bla".
query: get the other "specialiteiten" from that "bedrijf"
One method is to use a view.
If you want the values in a single delimited list, you can use group_concat():
SELECT bedrijfgegevens.id,
GROUP_CONCAT(CONCAT_WS(':', branches.naam, subbranches.naam, specialiteiten.naam))
FROM bedrijfgegevens INNER JOIN
subbranches
on subbranches.id = company.subbranche_id INNER JOIN
branches
on branches.id = subbranches.branche_id INNER JOIN
bedrijfgegevens_specialiteiten
on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id INNER JOIN
specialiteiten
on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
GROUP BY bedrijfgegevens.id
HAVING SUM(branches.naam = 'test' AND subbranches.naam = 'demo' AND specialiteiten.naam = 'bla') > 0;
Related
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
SELECT device.mac_address, device.serial_number, device.device_type, device.device_model, device.part_number, device.entitlement_id, device.platform_customer_id, application_customer.application_customer_id
FROM device
INNER JOIN device_to_app_customer
ON device.id = device_to_app_customer.device_id
INNER JOIN application_customer application_customer
ON device_to_app_customer.application_customer_id =
application_customer.id
WHERE application_customer.msp_app_customer_id = '3043'
UNION
SELECT distinct device.mac_address, device.serial_number, device.device_type, device.device_model, device.part_number, device.entitlement_id, device.platform_customer_id, null as application_customer_id
FROM device
INNER JOIN platform_customer
ON device.platform_customer_id = platform_customer.id
INNER JOIN folder
ON device.folder_id = folder.id
INNER JOIN application_customer application_customer
ON application_customer.platform_customer_id =
platform_customer.id
WHERE platform_customer.platform_customer_id = '660d6111877d413c9930be9e82c338b7'
AND folder.NAME = 'default';
I want to convert this to EntityGraph. I am not sure whether we have union for entitygraph.
Sorry, I'm a newbee here and can't get everyhing right at one time in beautiful code lists. I'm in a prephase to (let) launch my database on a website, but before I do I need to create a (total) view with inner join. And i get a message of the infamous 1054 unknown column in 'on clause'. Here is the actual MySQL script. Can someone help me succeeding the script as a view ? Around this code in the script it stucks (between the stars in the FROM SYNTAX): INNER JOIN zorgaanbieder AS zorgaanbieder_1 ON (zorgverlener.Zorgaanbieder_ID = zorgaanbieder_1.Zorgaanbieder_ID). Please see code below
CREATE VIEW `fetch_data` AS
SELECT
zorgactiviteiten.ZA_code,
zorgactiviteiten.ZA_naam,
zorgactiviteiten.ZA_omschr_consument,
zorgprofielklasse.ZPK_oms,
behandelingen.Behandeling_criteria,
aanspraak.Aanspraak_omschr,
aanspraak.Aanspraak_machtiging,
cluster.Cluster_naam,
diagnosen.Diagnose_code,
diagnosen.Diagnose_naam,
diagnosen.Diagnose_groep,
diagnosen.Diagnose_hoofdstuk,
specialisme.Specialisme_naam,
zorgproducten.ZP_code,
zorgproducten.ZP_naam,
zorgproducten.ZP_omschr_consument,
zorgproductgroepen.ZPG_naam,
declaratie.Declaratie_code,
declaratie.Declaratie_zorg,
contractueel.Contractueel_uitleg,
contract.Contract_zorg,
contract.Contract_verzekerd,
contract.Contract_uitsluiting,
zorgverzekeraar.Zorgverzekeraar_verzekeraar,
zorgverzekeraar.Zorgverzekering_verzekering,
zorgverzekeraar.Zorgverzekering_soort,
zorgverzekeraar.Zorgverzekering_zorgkeuze,
concern.Concern_inkoop,
concern.Concern_label,
zorgverlener.Zorgverlener_plaats,
zorgverlener.Zorgverlener_regio,
zorgverlener.Zorgverlener_soort,
zorgaanbieder.Zorgaanbieder_informatie,
zorgaanbieder.Zorgaanbieder_website,
zorgaanbieder.Zorgaanbieder_reviews,
zorgaanbieder.Zorgaanbieder_rapport,
zorgaanbieder.Zorgaanbieder_oordeel,
zorgaanbieder.Zorgaanbieder_prijslijst,
prijslijst.Prijslijst_tarief,
prijslijst.Prijslijst_actie,
prijslijst.Prijslijst_soort,
prijslijst.Prijslijst_jaar
FROM
concern_zorgaanbieder_prijslijst
INNER JOIN
prijslijst ON (concern_zorgaanbieder_prijslijst.Prijslijst_ID = prijslijst.Prijslijst_ID)
INNER JOIN
concern ON (concern_zorgaanbieder_prijslijst.Concern_ID = concern.Concern_ID)
INNER JOIN
zorgaanbieder ON (concern_zorgaanbieder_prijslijst.Zorgaanbieder_ID = zorgaanbieder.Zorgaanbieder_ID)
INNER JOIN
zorgaanbieder AS zorgaanbieder_1 ON (zorgverlener.Zorgaanbieder_ID = zorgaanbieder_1.Zorgaanbieder_ID)
INNER JOIN
zorgverlener ON (contract.Zorgverlener_koppel = zorgverlener.Zorgverlener_ID)
INNER JOIN
concern AS concern_1 ON (zorgverzekeraar.Concern_ID = concern_1.Concern_ID)
INNER JOIN
zorgverzekeraar ON (contract.Zorgverzekeraar_koppel = zorgverzekeraar.Zorgverzekeraar_ID)
INNER JOIN
contract ON (contractueel_contract.Contract_ID = contract.Contract_ID)
INNER JOIN
contractueel_contract ON (contractueel.Contractueel_ID = contractueel_contract.Contract_ID)
INNER JOIN
contractueel ON (contractueel_decaratie.Contractueel_ID = contractueel.Contractueel_ID)
INNER JOIN
contractueel_decaratie ON (declaratie.Declaratie_code = contractueel_decaratie.Declaratie_code)
INNER JOIN
declaratie ON (zorgproducten_declaratie.Declaratie_code = declaratie.Declaratie_code)
INNER JOIN
zorgproducten_declaratie ON (zorgproducten.ZP_code = zorgproducten_declaratie.ZP_code)
INNER JOIN
zorgproductgroepen ON (zorgproducten.ZPG_code = zorgproductgroepen.ZPG_code)
INNER JOIN
zorgproducten ON (dbc.ZP_code = zorgproducten.ZP_code)
INNER JOIN
specialisme ON (diagnosen.Specialisme_code = specialisme.Specialisme_code)
INNER JOIN
diagnosen ON (diagnosebereik.Diagnose_ID = diagnosen.Diagnose_ID)
INNER JOIN
diagnosebereik ON (dbc_diagnosebereik.Diagnosebereik_naam = diagnosebereik.Diagnosebereik_naam)
INNER JOIN
dbc_diagnosebereik ON (diagnosebereik.Diagnosebereik_naam = dbc_diagnosebereik.Diagnosebereik_naam)
INNER JOIN
dbc ON (dbc_diagnosebereik.DBC_koppel = dbc.DBC_koppel)
INNER JOIN
behandelingen ON (dbc.DBC_koppel = behandelingen.DBC_koppel)
INNER JOIN
cluster ON (behandelingen.Cluster_ID = cluster.Cluster_ID)
INNER JOIN
aanspraak ON (behandelingen.Aanspraak_code = aanspraak.Aanspraak_code)
INNER JOIN
zorgactiviteit_behandelingen ON (behandelingen.Behandeling_naam = zorgactiviteit_behandelingen.Behandeling_naam)
INNER JOIN
zorgactiviteiten ON (zorgactiviteit_behandelingen.ZA_code = zorgactiviteiten.ZA_code)
INNER JOIN
zorgprofielklasse ON (zorgactiviteiten.ZPK_code = zorgprofielklasse.ZPK_code)
Debugging advice: Start small, test, then by grow by small increments and re-test.
I believe you problem is INCORRECT SEQUENCE of joins, NOT incorrect column references. e.g.
## this will fail
select a.* from a
join c on b.id = c.id ## c attempts to join to b = incorrect sequence of joins
join b on a.id = b.id
Below, table zorgverlener attempts to join to contract but it has not yet been joined.
SELECT
czp.*
FROM concern_zorgaanbieder_prijslijst as czp
...
INNER JOIN zorgverlener ON (contract.Zorgverlener_koppel = zorgverlener.Zorgverlener_ID)
...
INNER JOIN contract ON (contractueel_contract.Contract_ID = contract.Contract_ID)
Other tips:
Do use table aliases
Don't use unnecessary parentheses
To correct that issue change the order of the joins:
SELECT
czp.*
FROM concern_zorgaanbieder_prijslijst as czp
...
INNER JOIN contract AS C ON contractueel_contract.Contract_ID = contract.Contract_ID
...
INNER JOIN zorgverlener ON c.Zorgverlener_koppel = zorgverlener.Zorgverlener_ID
but now we see that contractueel_contract needs to go above contract and so on it goes.
I believe a more correct sequence of jois is as follows, but there are 2 joins I cannot solve:
SELECT
czp.*
FROM concern_zorgaanbieder_prijslijst AS czp
INNER JOIN prijslijst ON czp.Prijslijst_ID = prijslijst.Prijslijst_ID
INNER JOIN concern ON czp.Concern_ID = concern.Concern_ID
## problems in the next 2 lines, seem to be recursve
INNER JOIN diagnosebereik ON dbc_diagnosebereik.Diagnosebereik_naam = diagnosebereik.Diagnosebereik_naam
INNER JOIN dbc_diagnosebereik ON diagnosebereik.Diagnosebereik_naam = dbc_diagnosebereik.Diagnosebereik_naam
INNER JOIN dbc ON dbc_diagnosebereik.DBC_koppel = dbc.DBC_koppel
INNER JOIN zorgproducten ON dbc.ZP_code = zorgproducten.ZP_code
INNER JOIN zorgproducten_declaratie ON zorgproducten.ZP_code = zorgproducten_declaratie.ZP_code
INNER JOIN declaratie ON zorgproducten_declaratie.Declaratie_code = declaratie.Declaratie_code
INNER JOIN contractueel_decaratie ON declaratie.Declaratie_code = contractueel_decaratie.Declaratie_code
INNER JOIN contractueel ON contractueel_decaratie.Contractueel_ID = contractueel.Contractueel_ID
INNER JOIN contractueel_contract ON contractueel.Contractueel_ID = contractueel_contract.Contract_ID
INNER JOIN contract ON contractueel_contract.Contract_ID = contract.Contract_ID
INNER JOIN zorgverlener ON contract.Zorgverlener_koppel = zorgverlener.Zorgverlener_ID
INNER JOIN zorgaanbieder ON czp.Zorgaanbieder_ID = zorgaanbieder.Zorgaanbieder_ID
INNER JOIN zorgaanbieder AS zorgaanbieder_1 ON zorgverlener.Zorgaanbieder_ID = zorgaanbieder_1.Zorgaanbieder_ID
INNER JOIN zorgverzekeraar ON contract.Zorgverzekeraar_koppel = zorgverzekeraar.Zorgverzekeraar_ID
INNER JOIN concern AS concern_1 ON zorgverzekeraar.Concern_ID = concern_1.Concern_ID
INNER JOIN zorgproducten_declaratie ON zorgproducten.ZP_code = zorgproducten_declaratie.ZP_code
INNER JOIN zorgproductgroepen ON zorgproducten.ZPG_code = zorgproductgroepen.ZPG_code
INNER JOIN diagnosen ON diagnosebereik.Diagnose_ID = diagnosen.Diagnose_ID
INNER JOIN specialisme ON diagnosen.Specialisme_code = specialisme.Specialisme_code
INNER JOIN behandelingen ON dbc.DBC_koppel = behandelingen.DBC_koppel
INNER JOIN cluster ON behandelingen.Cluster_ID = cluster.Cluster_ID
INNER JOIN aanspraak ON behandelingen.Aanspraak_code = aanspraak.Aanspraak_code
INNER JOIN zorgactiviteit_behandelingen ON behandelingen.Behandeling_naam = zorgactiviteit_behandelingen.Behandeling_naam
INNER JOIN zorgactiviteiten ON zorgactiviteit_behandelingen.ZA_code = zorgactiviteiten.ZA_code
INNER JOIN zorgprofielklasse ON zorgactiviteiten.ZPK_code = zorgprofielklasse.ZPK_code
I have an SQL query like below which has a where clause as WHERE pg.active=1 AND tcg.active=1 AND tpg.active=1,issue right now is that one or all of pg.active tcg.active ,tpg.active is set to 1 at any time,in the case where one of these is not set to 1 query doesn't return anything.
How do I change the where clause to return if any of pg.active tcg.active ,tpg.active is set to 1?
SELECT
..........
FROM software_products_software_images spsi
INNER JOIN software_images si ON si.software_image_id = spsi.software_image_id
INNER JOIN software_products sp ON sp.id = spsi.software_product_id
LEFT JOIN software_products_software_images_testplan_gate tpg ON tpg.software_products_software_images_id = spsi.Id
LEFT JOIN test_suites tp ON tp.id = tpg.testplan_id
LEFT JOIN software_products_software_images_testcase_gate tcg ON tcg.software_products_software_images_id = spsi.Id
LEFT JOIN test_cases tc ON tc.id = tcg.testcase_id
LEFT JOIN test_suites ts ON ts.id = tc.test_suite_id
LEFT JOIN software_products_software_images_percentage_gate pg ON pg.software_products_software_images_id = spsi.Id
...........................
WHERE pg.active=1 AND tcg.active=1 AND tpg.active=1
AND si.software_image='NHSS.QSDK.7.0.1' ORDER BY si.software_image, ts.suite_name
I believe you need to encapsulate the OR part of the WHERE statement in round brackets so that it returns a TRUE on any OR match along with your other conditions.
SELECT
..........
FROM software_products_software_images spsi
INNER JOIN software_images si ON si.software_image_id = spsi.software_image_id
INNER JOIN software_products sp ON sp.id = spsi.software_product_id
LEFT JOIN software_products_software_images_testplan_gate tpg ON tpg.software_products_software_images_id = spsi.Id
LEFT JOIN test_suites tp ON tp.id = tpg.testplan_id
LEFT JOIN software_products_software_images_testcase_gate tcg ON tcg.software_products_software_images_id = spsi.Id
LEFT JOIN test_cases tc ON tc.id = tcg.testcase_id
LEFT JOIN test_suites ts ON ts.id = tc.test_suite_id
LEFT JOIN software_products_software_images_percentage_gate pg ON pg.software_products_software_images_id = spsi.Id
...........................
WHERE (pg.active=1 OR tcg.active=1 OR tpg.active=1)
AND si.software_image='NHSS.QSDK.7.0.1' ORDER BY si.software_image, ts.suite_name
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;