pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
A Not Join
4+ messages / 4 participants
[nested] [flat]

* A Not Join
@ 2005-11-01 14:27  L van der Walt <mailing@lani.co.za>
  0 siblings, 3 replies; 4+ messages in thread

From: L van der Walt @ 2005-11-01 14:27 UTC (permalink / raw)
  To: pgsql-sql; pgsql-general@postgresql.org

I have three table:
Users - Contains username, ID etc...
Permissions - A permission name and ID
Link up table - The user.id and permission.id

If a user.id and a permission.id row exists in the linkuptable the user 
have that permission granted.

With the statement below I can see the permissions a user have.
 
SELECT users.username, permissions.name
FROM users INNER JOIN linkuptable
  ON (users.id = linkuptable.userid)
INNER JOIN permissions
  ON (permissions.id = linkuptable.permissionid)
WHERE users.username = 'DummyUser'

How do I see the permissions that user DON'T have with a fast SQL statement.

Thus, a NOT the statement for the above SQL statement

Regards

Lani




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: A Not Join
@ 2005-11-01 15:15  Jim C. Nasby <jnasby@pervasive.com>
  parent: L van der Walt <mailing@lani.co.za>
  2 siblings, 0 replies; 4+ messages in thread

From: Jim C. Nasby @ 2005-11-01 15:15 UTC (permalink / raw)
  To: L van der Walt <mailing@lani.co.za>; +Cc: pgsql-sql; pgsql-general@postgresql.org

On Tue, Nov 01, 2005 at 04:27:01PM +0200, L van der Walt wrote:
> I have three table:
> Users - Contains username, ID etc...
> Permissions - A permission name and ID
> Link up table - The user.id and permission.id
> 
> If a user.id and a permission.id row exists in the linkuptable the user 
> have that permission granted.
> 
> With the statement below I can see the permissions a user have.
> 
> SELECT users.username, permissions.name
> FROM users INNER JOIN linkuptable
>  ON (users.id = linkuptable.userid)
> INNER JOIN permissions
>  ON (permissions.id = linkuptable.permissionid)
> WHERE users.username = 'DummyUser'
> 
> How do I see the permissions that user DON'T have with a fast SQL statement.
> 
> Thus, a NOT the statement for the above SQL statement

LEFT JOIN permissions ON (...)
WHERE permissions.id IS NULL

You might have to do the NULL check in a HAVING clause instead... try
it.

BTW, this is probably better asked on pgsql-sql.
-- 
Jim C. Nasby, Sr. Engineering Consultant      jnasby@pervasive.com
Pervasive Software      http://pervasive.com    work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf       cell: 512-569-9461



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: A Not Join
@ 2005-11-02 02:28  Jerry Sievers <jerry@jerrysievers.com>
  parent: L van der Walt <mailing@lani.co.za>
  2 siblings, 0 replies; 4+ messages in thread

From: Jerry Sievers @ 2005-11-02 02:28 UTC (permalink / raw)
  To: pgsql-general@postgresql.org

L van der Walt <mailing@lani.co.za> writes:

> I have three table:
> Users - Contains username, ID etc...
> Permissions - A permission name and ID
> Link up table - The user.id and permission.id
> 
> If a user.id and a permission.id row exists in the linkuptable the user 
> have that permission granted.
> 
> With the statement below I can see the permissions a user have.
>  
> SELECT users.username, permissions.name
> FROM users INNER JOIN linkuptable
>   ON (users.id = linkuptable.userid)
> INNER JOIN permissions
>   ON (permissions.id = linkuptable.permissionid)
> WHERE users.username = 'DummyUser'
> 
> How do I see the permissions that user DON'T have with a fast SQL statement.
> 

Simple.

select permname
from permissions
where permid not in (
	select permid	
from linkage
	where userid = 'dummy'
);

Or... a slick way to get the anti-permission for the whole bunch of
users is to;

cross join the userids with permids and then EXCEPT SELECT from
linkage table to filter out the active permissions.

Have fun!


-- 
-------------------------------------------------------------------------------
Jerry Sievers   305 854-3001 (home)     WWW ECommerce Consultant
                305 321-1144 (mobile	http://www.JerrySievers.com/



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: A Not Join
@ 2005-11-21 20:16  stig erikson <stigerikson_nospam_@yahoo.se>
  parent: L van der Walt <mailing@lani.co.za>
  2 siblings, 0 replies; 4+ messages in thread

From: stig erikson @ 2005-11-21 20:16 UTC (permalink / raw)
  To: pgsql-sql

L van der Walt wrote:
> I have three table:
> Users - Contains username, ID etc...
> Permissions - A permission name and ID
> Link up table - The user.id and permission.id
> 
> If a user.id and a permission.id row exists in the linkuptable the user
> have that permission granted.
> 
> With the statement below I can see the permissions a user have.
> 
> SELECT users.username, permissions.name
> FROM users INNER JOIN linkuptable
>  ON (users.id = linkuptable.userid)
> INNER JOIN permissions
>  ON (permissions.id = linkuptable.permissionid)
> WHERE users.username = 'DummyUser'
> 
> How do I see the permissions that user DON'T have with a fast SQL
> statement.
> 
> Thus, a NOT the statement for the above SQL statement
> 
> Regards
> 
> Lani
> 

what you need to do is select all possible permissions and then remove the
permissions that exist. try somthing like:

SELECT permissions.name
FROM permissions.name
WHERE permission.permissionid NOT IN
	( SELECT permissions.permissionid
	  FROM users INNER JOIN linkuptable
	  ON (users.id = linkuptable.userid)
	  INNER JOIN permissions
	  ON (permissions.id = linkuptable.permissionid)
	  WHERE users.username = 'DummyUser'
	)




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2005-11-21 20:16 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-11-01 14:27 A Not Join L van der Walt <mailing@lani.co.za>
2005-11-01 15:15 ` Jim C. Nasby <jnasby@pervasive.com>
2005-11-02 02:28 ` Jerry Sievers <jerry@jerrysievers.com>
2005-11-21 20:16 ` stig erikson <stigerikson_nospam_@yahoo.se>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox