| View previous topic :: View next topic |
| Author |
Message |
Philip Marlowe Power User

Joined: 15 May 2011 Posts: 94
|
Posted: Sat Jun 30, 2012 12:45 am Post subject: sql problem |
|
|
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 |
|
 |
F3K Total General User

Joined: 12 May 2012 Posts: 5
|
Posted: Sun Jul 08, 2012 12:24 am Post subject: |
|
|
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 |
|
 |
|
|
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
|