简体   繁体   中英

column alias oracle 10g

This seems so simple but I can't understand how do I do this query: I have a table users like this:

user_id | name | role
1       | abc  | a
2       | lol  | b
3       | f    | c

and a table usersprojects (with user and project PKs)

projectid | userid
1         | 1
1         | 2
2         | 2
2         | 3

How could I select all users columns and also a boolean column alias "assigned" to project "1"

I would like a result like this:

user_id | name | role | assigned
1       | abc  | a    | true
2       | lol  | b    | true
3       | f    | c    | false

The query wouldn't be something like this:

 Select user_id ,name, role,
  (users.user_id in (Select user_id from usersprojects where projectid=1)
    ) assigned;

But it doesn't work... what is the right way to do this query?

 SELECT u.user_id ,name, role, NVL(projectId, 0) assigned
 FROM users u LEFT JOIN userprojects up ON (u.user_id = up.userid)
SELECT
    user_id, name, role,
    CASE WHEN (SELECT COUNT(*) FROM UsersProjects up #
               WHERE up.user_id = u.user_id) > 0 
         THEN 'true' ELSE 'false' END assigned
FROM Users u

You need a left outer join. Keep in mind that there is no boolean data type in Oracle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM