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
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
The first request make my server crash (The CPU go to 100%)
SELECT s.idSinistre
FROM test_apc_sinistres s
INNER JOIN apc_correspondances c ON c.idLiaison = s.idSinistre AND c.refCategorie = '69.1'
INNER JOIN apc_procedures_taches_sinistres p ON s.idSinistre = p.idSinistre
INNER JOIN apc_contacts co ON s.idAdb = co.idContact
INNER JOIN apc_parametres_adb pa ON pa.idAdb = co.idContact
WHERE s.refStatut = '62.2'
GROUP BY s.idSinistre;
select co.Nom, s.idSinistre, count(c.idMessage) as nbMEssage, count(p.id) as nbProc
from test_apc_sinistres s
inner join apc_correspondances c on c.idLiaison = s.idSinistre and c.refCategorie = '69.1'
inner join apc_procedures_taches_sinistres p on s.idSinistre = p.idSinistre
inner join apc_contacts co on s.idAdb = co.idContact
inner join apc_parametres_adb pa on pa.idAdb = co.idContact
where s.refStatut = '62.2'
group by s.idSinistre;
The only difference between this two is the data i select. Someone already have this issue?
Just re-check your columns one by one and you'll see that you are doing aggregates on all except one column.
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;
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 have
SELECT
servisler.geo_location,
servisler.ADRES_MERKEZ,
servisler.ADRES_ILCE,
servisler.ADRES_IL,
servisler.FIRMA_UNVANI,
servisler.ADRES_ISTEL,
servisler.YETKILI_ADISOYADI,
urun_gruplari.GRUP_ADI
FROM
servisler
INNER JOIN urun_gruplari ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
INNER JOIN kullanici ON kullanici.SERVIS_RECNO = servisler.RECNO
INNER JOIN kullanici_cihaz ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO AND kullanici_cihaz.URUN_GRUP_NO = urun_gruplari.RECNO
where kullanici.kullanici = 'MAR.EDI.003'
but it says
[Err] 1054 - Unknown column 'kullanici_cihaz.URUN_GRUP_NO' in 'on clause'
enen though the column exits. What is its problem?
schema
Server version: 5.1.33-community-log
I think this is because you're using the kullanici_cihaz table in your ON clause, and that before this table is linked.
Try to rearrange your INNER JOIN :
SELECT
servisler.geo_location,
servisler.ADRES_MERKEZ,
servisler.ADRES_ILCE,
servisler.ADRES_IL,
servisler.FIRMA_UNVANI,
servisler.ADRES_ISTEL,
servisler.YETKILI_ADISOYADI,
urun_gruplari.GRUP_ADI
FROM
servisler
INNER JOIN kullanici ON kullanici.SERVIS_RECNO = servisler.RECNO
INNER JOIN kullanici_cihaz ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO
INNER JOIN urun_gruplari ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
WHERE
kullanici.kullanici = 'MAR.EDI.003'
EDIT
You also have some 'loop inner join', I removed it :
INNER JOIN urun_gruplari ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
INNER JOIN kullanici_cihaz ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO AND kullanici_cihaz.URUN_GRUP_NO = urun_gruplari.RECNO
Because kullanici.SERVIS_RECNO does not exist when you join the tables: servisler, urun_gruplari. Join table kullanici first before joining urun_gruplari.
SELECT servisler.geo_location,
servisler.ADRES_MERKEZ,
servisler.ADRES_ILCE,
servisler.ADRES_IL,
servisler.FIRMA_UNVANI,
servisler.ADRES_ISTEL,
servisler.YETKILI_ADISOYADI,
urun_gruplari.GRUP_ADI
FROM servisler
INNER JOIN kullanici
ON kullanici.SERVIS_RECNO = servisler.RECNO
INNER JOIN urun_gruplari
ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
INNER JOIN kullanici_cihaz
ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO AND
kullanici_cihaz.URUN_GRUP_NO = urun_gruplari.RECNO
where kullanici.kullanici = 'MAR.EDI.003'