OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

sql problem

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Base
View previous topic :: View next topic  
Author Message
Philip Marlowe
Power User
Power User


Joined: 15 May 2011
Posts: 94

PostPosted: Sat Jun 30, 2012 12:45 am    Post subject: sql problem Reply with quote

i've two tables:

FRUIT=(ID_FRUIT PK, FRUIT)
example: 0,apple ; 1,banana ; 2,apricot ; 3,melon ; 4,melon ; 5,pear

REL=(ID_REL PK, FRUIT, ID_GROUP) grouping fruits togheter (fruits can stay in more groups or none)
example: 0,apple,a ; 1,pear,a ; 2,melon,b ; 3,apple,b ;4,apricot,b

what's the simplest way to write a query selecting ID_FRUIT,ID_GROUP where i want couples (fruit - group in which it fits given relations defined in REL table) but if fruit don't fit in any group i want the record id_fruit,'no' to be selected/created.
the result of the query will be (in the example above):

0,A ; 0,B ; 1,no ; 2,B ; 3,B ; 4,B ; 5,A

because banana doesn't fit in any group!
_________________
Libreoffice Base HSQLDB not Embedded
Back to top
View user's profile Send private message Send e-mail
F3K Total
General User
General User


Joined: 12 May 2012
Posts: 36

PostPosted: Sun Jul 08, 2012 12:24 am    Post subject: Reply with quote

Hi,
i changed REL, put a FK (FK_ID_FRUIT) instead of FRUIT, because i was too lazy to type in the fruits twice, but i think this works:

Code:
SELECT "ID_FRUIT", coalesce("ID_GROUP",'no') as "ID_GROUP" FROM "REL" RIGHT OUTER JOIN (SELECT "ID_FRUIT" FROM "FRUIT") ON "FK_ID_FRUIT" = "ID_FRUIT"


greats R
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Base All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group