I know that I have seen a couple of other questions about this error but I'm new to the sql JOIN so plz could you guy explain what I'm doing wrong.
Here's my query
SELECT Klanten.Klantnummer,`Barcode`, `Naam`, `BetaalStatus`, `ScanStatus`, `TijdScan`, `Prijs`
FROM `Klanten`, `kaart`
LEFT JOIN (`Intro`)
ON (Intro.KlantNummer = Klanten.Klantnummer)
WHERE kaart.KlantNummer = Klanten.Klantnummer
This is the Error I get like you have seen in the title
1054 - Unknown column 'Klanten.Klantnummer' in 'on clause'
And the db names are correct
Simple rule: Never use commas in the FROM clause. Always use proper, explicit JOIN syntax. If you did that, you would not have an error:
SELECT Klanten.Klantnummer,`Barcode`, `Naam`, `BetaalStatus`, `ScanStatus`, `TijdScan`, `Prijs`
FROM `Klanten` JOIN
`kaart`
ON kaart.KlantNummer = Klanten.Klantnummer LEFT JOIN
`Intro`
ON Intro.KlantNummer = Klanten.Klantnummer ;
The problem is that the precedence of , and JOIN are different. Hence, the table before the comma is not known to the ON clause.
Related
I've the following subquery in an sql query:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, NOMBRE AS NOMBREUNIDAD FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY ID_PLAN, ID_CURSO, NEDICION) ASIS
Problem I have I believe lies in that both table ALUMNOS and UNIDADES have a column named 'NOMBRE' so if I attempt to execute the query I obtain:
00000 - "column ambiguously defined"
To avoid that I thought about changing NOMBRE AS NOMBREUNIDAD to:
UNIDADES.NOMBRE AS NOMBREUNIDAD
But if I do that I get a:
00000 - "not a GROUP BY expression"
So, I don't know what to do so that subquery executes properly.
What should I change to properly execute query without changing the column name?
Aliases are pretty useful, if you use them. The simplify queries and make them easier to read and maintain. I'd suggest you to do so, as it'll also help query to work because Oracle doesn't know which table you actually meant when you selected those 4 columns - which tables do they belong to?
This is just a guess as I don't know your tables so you'll have to fix it yourself. Also, I literally JOINed tables; try to avoid comma-separating them in FROM clause and doing join in WHERE clause as it is supposed to filter data.
GROUP BY, as already commented, is probably useless. If you wanted to fetch distinct set of values, then use appropriate keyword: distinct.
SELECT DISTINCT n.id_plan,
s.id_curso,
u.nedicion,
u.nombre
FROM asisten n
JOIN alumnos s ON n.cod = s.cod
JOIN unidades u
ON u.idestructura = s.idestructura
AND u.cdundorg = s.cdundorg
WHERE UPPER (TRANSLATE (u.nombre, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
I managed to solve my problem:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, UNIDADES.NOMBRE AS NOMBREUNIDAD
FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY UNIDADES.NOMBRE,ID_PLAN, ID_CURSO, NEDICION
)
I've tried to write a SQL statement to select from some tables. But when I run it, I get an error, and I don't know how to fix it.
When I just select from vertrag and join pgrdat, abrkreis, mandant and komm_dat, everything works fine.
However, when I try to add literal into the joins, I get the following error:
ORA-00904: "VERTRAG"."MAN" invalid identifier
on (left join literal on literal.lit_kzl = pgrdat.beruftit and literal.man = vertrag.man)
or
ORA-00918: column ambiguously defined
on (left join Literal on Literal.LIT_KZL=pgrdat.BERUFTIT and Literal.man=man)
literal.man and vertrag.man both exist.
Here's my SQL:
SELECT man,
ak,
pnr,
vertrag.vertnr,
vertrag.eintrt2,
vertrag.ma_ab,
vertrag.verbegin,
vertrag.ver_ab,
vertrag.ver_bis,
vertrag.verende,
vertrag.enlogru,
pgrdat.spr,
pgrdat.anrede,
pgrdat.auswnr,
pgrdat.beruftit,
pgrdat.daschudat,
pgrdat.fax,
pgrdat.gebdat,
pgrdat.gebname,
pgrdat.gebort,
pgrdat.geschl,
pgrdat.lnd,
pgrdat.miname,
pgrdat.namevor,
pgrdat.namezus,
pgrdat.naname,
pgrdat.ort,
pgrdat.plz,
pgrdat.plzfach,
pgrdat.postfach,
pgrdat.pst_ab,
pgrdat.pst_bis,
pgrdat.staat,
pgrdat.staat2,
pgrdat.strasse,
pgrdat.telgesch,
pgrdat.telprivat,
pgrdat.titel,
pgrdat.vorname,
pgrdat.empfaenger,
pgrdat.taetint,
pgrdat.zimmer,
pgrdat.sachbegrp,
pgrdat.logasach,
pgrdat.stellung,
pgrdat.logasach2,
pgrdat.sachbegrp2,
abrkreis.ak_bez,
abrkreis.ak_kurz,
abrkreis.ak_ort,
abrkreis.ak_plz,
abrkreis.ak_fax,
abrkreis.ak_plzfach,
abrkreis.ak_postfach,
abrkreis.ak_strasse,
abrkreis.ak_telefon,
abrkreis.ak_text,
mandant.man_bez,
mandant.man_fax,
mandant.man_firma,
mandant.man_kurz,
mandant.man_ort,
mandant.man_plzfach,
mandant.man_plzstr,
mandant.man_postfach,
mandant.man_st_nr,
mandant.man_strasse,
mandant.man_telefon,
komm_dat.km_art,
komm_dat.km_ab,
komm_dat.km_bis,
komm_dat.km_bem,
literal.lit_txt
FROM vertrag
JOIN pgrdat
USING (man, ak, pnr)
JOIN abrkreis
USING (man, ak)
JOIN mandant
USING (man)
LEFT JOIN komm_dat
USING (man, ak, pnr)
LEFT JOIN literal
ON literal.lit_kzl = pgrdat.beruftit
AND literal.man = man
WHERE superman IN ('41900', '41901', '41902', '41903')
AND literal.lit_art = 'BERUFTIT'
AND ver_ab <= trunc(SYSDATE)
AND pgrdat.pst_ab <= trunc(SYSDATE)
AND ((ver_bis >= trunc(SYSDATE) AND pgrdat.pst_bis >= trunc(SYSDATE)) OR (ver_bis IS NULL AND pgrdat.pst_bis IS NULL));
The problem is with mixing using and on join syntax. There is a third variant you didn't try:
left join Literal on Literal.LIT_KZL=pgrdat.BERUFTIT and Literal.man=komm_dat.man
which gets
ORA-25154: column part of USING clause cannot have qualifier
You can't use the unqualified man because it's ambiguous (appearing in several tables); you can't use a qualifier because it's been used in earlier using() clauses.
If it was an inner join you could change that Literal join to using (man) and move the lit_kzl check to the where clause, but as it's an outer join that won't work (or at least, would force it back to being an inner join).
You probably need to change all the other using clauses to on, unfortunately:
...
from vertrag
join pgrdat on pgrdat.man = vertrag.man and pgrdat.ak = vertrag.ak and pgrdat.pnr = vertrag.pnr
join abrkreis on abrkreis.man = vertrag.man and abrkreis.ak = vertrag.ak
join mandant on mandant.man = vertrag.man
left join komm_dat on komm_dat.man = vertrag.man and komm_dat.ak = vertrag.ak and komm_dat.pnr = vertrag.pnr
left join Literal on Literal.LIT_KZL=pgrdat.BERUFTIT and Literal.man=vertrag.man
and Literal.LIT_ART='BERUFTIT'
where ...
I've moved and Literal.LIT_ART='BERUFTIT' from the where clause into the join condition, as that would also have forced that outer join to become an inner join again.
Below code works fine:
$stmt = $DB_con->prepare("select tbl_items.*,tbl_basket.* from tbl_items INNER JOIN tbl_basket on tbl_basket.id_items = tbl_items.id
where tbl_basket.cookie_user = 100);
I get en error when I change it to this:
$stmt = $DB_con->prepare("select tbl_items.*,tbl_basket.* from tbl_items
INNER JOIN tbl_basket on tbl_basket.id_items = tbl_items.id
where tbl_basket.cookie_user = c2b32bbfd582389b7df8e89e5796aa27);
.
Error: Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not
found: 1054 Unknown column 'c2b32bbfd582389b7df8e89e5796aa28' in 'where clause' in
I think the problem is with inner join, However it works perfectly without inner join.
any solution?
Your working query suggests, that cookie_user is of a numeric type. If so, prepend 0x to the hexadecimal representation. That should work.
...
tbl_basket.cookie_user = 0xc2b32bbfd582389b7df8e89e5796aa27
...
I am getting a missing operator error in the following SQL statement:
SELECT Sample.Number, Sample.SampleDate, BOD.BOD_Concentration_IN, BOD.BOD_Concentration_OUT, TSS.TSS_Influent, TSS.TSS_Effluent
FROM SampleInformation as Sample
INNER JOIN BOD_Data as BOD ON Sample.Number = BOD.Number
INNER JOIN TSS_Data as TSS ON Sample.Number = TSS.Number
WHERE (DATEPART('m',Sample.SampleDate) = DATEPART('m',#1/13/2016 12:01:00 PM#))
AND (DATEPART('yyyy',Sample.SampleDate) = DATEPART('yyyy',#1/13/2016 12:01:00 PM#)) ORDER BY Sample.SampleDate
I eliminated the "WHERE" clause and still got the error, so it must be in the join.
Can anyone see what I am missing here? Thanks!
I finally figured out you are missing ( ) around the 'FROM' clause... try the following:
And BTW, 'Number' does not need to be enclosed in brackets (although it is a reserved word)
SELECT Sample.Number, Sample.SampleDate, BOD.BOD_Concentration_IN, BOD.BOD_Concentration_OUT, TSS.TSS_Influent, TSS.TSS_Effluent
FROM (SampleInformation AS Sample
INNER JOIN BOD_Data AS BOD ON Sample.Number = BOD.Number)
INNER JOIN TSS_Data AS TSS ON Sample.Number = TSS.Number
WHERE (DATEPART('m',Sample.SampleDate) = DATEPART('m',#1/13/2016 12:01:00 PM#))
AND (DATEPART('yyyy',Sample.SampleDate) = DATEPART('yyyy',#1/13/2016 12:01:00 PM#)) ORDER BY Sample.SampleDate
please tell me what is the syntax problem in this query
SELECT sde
FROM TABLE_EW sde , CASE_W spr, DOCUMENT swp
JOIN swp.id, swp.YEAR ON (swp.id = sde.ID_DOCUMENT)
JOIN spr.ID, spr.STATE, spr.NUMBER ON (spr.ID_DOCUMENT = swp.ID)
WHERE sde.IDENT_TABLEEW LIKE '122337456464'
AND swp.YEAR LIKE 2015;
SQLDeleoper point problem to From Line
I think this is the query you meant to write:
SELECT sde.*,swp.id, swp.YEAR,spr.ID, spr.STATE
FROM TABLE_EW sde
JOIN DOCUMENT swp ON (swp.id = sde.ID_DOCUMENT)
JOIN CASE_W spr ON (spr.ID_DOCUMENT = swp.ID)
WHERE sde.IDENT_TABLEEW = '122337456464'
AND swp.YEAR = 2015;
As mentioned in the comments, you have A LOT of errors in your SQL code.
You use implicit and explicit joins together, AVOID the use of implicit joins syntax and use only the proper syntax like my example.
Also, only in the select you can specify the columns you want, I'm guessing what you've been trying to do is
JOIN spr.ID, spr.STATE -> wanted this columns.
You should write them in the select part.
Another problem is the join condition, you either use implicit joins, (from table,table2,table3..) and then the join condition is in the where clause or you use explicit joins and then the condition is in the ON clause. You can't use both!
Another problem is the unnecessary use of LIKE . When comparing to an exact match, use EQUALS sign.