SQL the fuck?

Why is this query giving an "invalid column reference" error?
Quote
SELECT x.adcode, x.naamsport, count(y.lnr)
FROM Afdeling x, Bestuursafdeling y
WHERE x.adcode = y.adcode


Quote by Database create queries
CREATE TABLE Lid
( lnr smallint NOT NULL,
naam char(20),
adres char(30),
woonplaats char(30),
geboortedatum DATE,
geslacht char(5),
PRIMARY KEY (lnr)
);

CREATE TABLE Afdeling
( adcode float NOT NULL,
naamsport char(20),
oprichtdatum date,
PRIMARY KEY (adcode)
);

CREATE TABLE Bestuursafdeling
( functie char(20),
lnr smallint NOT NULL,
adcode float NOT NULL,
PRIMARY KEY (lnr, adcode),
FOREIGN KEY (lnr) REFERENCES Lid (lnr),
FOREIGN KEY (adcode) REFERENCES Afdeling (adcode)
);


better view of the queries
Comments
19
change line "FROM Afdeling x, Bestuursafdeling y"
to "FROM Afdeling as x, Bestuursafdeling as y"

EDIT: also looking at your first line, I am not sure if you will get desired result with your count(y.lnt). It looks to be used a bit strange (eng column/table names would actually increase readability, now I am not sure what you are trying to do)
you rly are fucking smart arent you ..
Parent
I want the amount of "users" in a certain "department"

users as in lnr
department as in adcode(department code) and naamsport(department name)

edit:

Quote
Dynamic SQL Error
SQL error code = -104
Token unknown - line 2, char 13
as


when I tried your version
Parent
well, depends on what sql engine you are running, I am using 5.0.7, tried your query and it actually runs without any problem.
But, there can be one more thing, first try to run query without count, if it runs ok, you are probably missing GROUP BY statement, if it wont, well, there is smth strange then :D
Parent
I forgot to add the GROUP BY (I have it in my original)
anyway, im a bit embarrassed to tell you but im using Interbase from the year 1999 or smth

which program are you running?
Quote
SELECT count(y.lnr)
FROM Bestuursafdeling y, Afdeling x
WHERE y.adcode = x.adcode
GROUP BY x.adcode

works but when I add x.adcode and x.naamsport it doesnt
Parent
I am running my own MySQL server together with Apache server, if you want easy installation and setup, download XAMPP, its really easy.
btw, error code -104 is smth about syntax error, to be more specific invalid character appears there where some reserved word is expected.

QuoteSELECT `x`.`adcode` as `adcode`, `x`.`naamsport` as `naamsport`, count(`y`.`lnr`) as `count`
FROM `Afdeling` as `x`, `Bestuursafdeling` as `y`
WHERE `x`.`adcode` = `y`.`adcode`
GROUP BY `x`.`adcode`;

try this, if this wont work, I guess there is smth wrong with your database script interpreter.
Parent
I guess my shit is too old to understand that.
It gives errors for the ' tokens, also for "as"


Thanks man, you have been a great help.
Ill just hand these in and tell him this system is too old.
Parent
there is one more thing you can try, and I guess it should be ok even for year 1999 :P
first, you have to check whether your table are really starting with uppercase (in my database, they got created with lowercase eventhought I used very same sql commands you provided here, but my db seems to be case insensitive, so it doesnt rly matter)

QuoteSELECT Afdeling.adcode, Afdeling.naamsport, count(Bestuursafdeling.lnr)
FROM Afdeling INNER JOIN Bestuursafdeling
ON Afdeling.adcode = Bestuursafdeling.adcode
GROUP BY Afdeling.adcode;


try this, there is no aliasing and there is fully written join statement.
Parent
my database isnt casesensitive either
it still gives me: invalid column reference

Thx for your efforts man but I guess my sql program is just outdated

im gonna send u a pm, could u check that one out for me too?
Parent
code in english
Hello World.
Use english maybe?
dont you need to use a join command when combining two different tables?

otherwise theres something wrong with ur collumns as the error suggest, try looking at capitals etc as its often case sensitive
FROM Afdeling x, Bestuursafdeling y
WHERE x.adcode = y.adcode

is shorthand for

FROM Afdeling x INNER JOIN Bestuursafdeling y
ON x.adcode = y.adcode

so that shouldnt really matter. But as he said he is using db from 1999, I wouldnt be surprised its bithing about it, SQL standarts back then had to be kinda different from todays.
Parent
stupid that they let me learn the JOIN function then :(

e: no need to write INNER btw :P
Parent
on contrary, you should actually be glad that you know the mechanics of join and what actually stands behind that shorthand code. And also sometimes you need LEFT/RIGHT/OUTER JOINs too, and there is no short form for those (i guess).
Parent
all i learned is that i need left / right and just join x]
Parent
ICT nerdje.
Back to top