开发者

postgreSQL inner join

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 er开发者_如何学运维ror missing FROM-clause entry for table "roles"

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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜