views:

211

answers:

3

i have sql query

select * from "Roles"  Join "Users"  On "Roles".Role="Users".RoleId

it return error column Roles.role does not exist

query

select * from "Roles"  Join "Users"  On Roles.Role=Users.RoleId

return error missing FROM-clause entry for table "roles"

how can i solve this problem? i aways work with ms sql

A: 

write it like this way:

select * from Roles  INNER JOIN Users  On (Roles.Role= Users.RoleId)

check INNER JOIN two tables for more info...

or you can use the simple way that it works with most DBMS:

select * from Roles,Users where Roles.Role= Users.RoleId
Wael Dalloul
hey men, if i write 'select * from Roles INNER JOIN Users On (Roles.Role= Users.RoleId)' it gives error 'relation "roles" does not exist'i sad i worked with ms sql so i know how work inner join
kusanagi
+1  A: 

You cannot use the name Roles in the join condition. Internally all table names like Roles, ROLES, roles, RoLeS are converted into roles (lower case). When you use "Roles", "ROLES", "roles", "RoLeS" the name is used exactly as you've written that (no lower case convertion) so in the FROM part are taken "Roles" and "Users" tables and in the join condition the table names are roles and users and such tabbles don't exist.

The simples way is to use only table names without "", just use simple Roles instead of "Roles" so you can write Roles or roles regardless the letters lower/upper case.

Simon
f**k so i can't use big letters like Users or name fields like Surname? only with small letters?
kusanagi
You can, but then you have to use "" all the time, so if you just create table "UsERS" then you have to relate that as the "UsERS" table. Much easier is just to use Users or users.
Simon
The relevant section of the Postgres' docs - http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Milen A. Radev
A: 

I suspect you needed to write "Roles"."Role" = "Users"."RoleId", because it's complaining about not being able to find the lower-case column name.

As others have mentioned, it's usually easiest to create everything as lower case, even if you use mixed case in queries: so if the table is called "roles" and the column "role" etc. you can write Roles.Role = Users.RoleId and the identifiers will be converted to lower case behind the scenes.

araqnid