File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Outer join headache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Outer join headache" Watch "Outer join headache" New topic

Outer join headache

Andy Westley

Joined: Nov 09, 2004
Posts: 19
Hi there,

I'm working on an application that will register when users see particular things. Some of the database is outlined below:

User: id, name
Catalog: id, name
CatalogItem: id, name, catalog_id
Spotted: user_id, catalog_item_id, spotted_date

So, a Catalog is a collection of CatalogItems (eg, Planes would contain 747, 767, DC10, etc). Spotted registers when an individual user has seen one of the CatalogItems. What I was hoping to do was put together a query that returns me a list of all the CatalogItems with the date spotted, if it had been. The closest I've got is the following:

(SELECT `catalog_item`.`id`, `catalog_item`.`name`, `spied`.`spied` FROM `user`, `catalog`, `catalog_item`, `spied` WHERE`user`.`id` = 3 AND `spied`.`user_id` = `user`.`id` AND `spied`.`catalog_item_id` = `catalog_item`.`id` AND `catalog_item`.`catalog_id` = `catalog`.`id` AND`catalog`.`id` = 1)
(SELECT `catalog_item`.`id`, `catalog_item`.`name`, 'xxx' as spied FROM `catalog`, `catalog_item` WHERE`catalog_item`.`catalog_id` = `catalog`.`id` AND `catalog`.`id` = 1)
ORDER BY `name`

That seems a bit heavy handed and would still leave me having to filter out the duplicate (items that have been spotted from the second sub-query) in some code.

It feels like an outer join should be the order of the day, but I've tried and I can't write one correctly that (left?) outer joins CatalogItem and Spotted, and maintains the rest of the conditions. Everything I've tried is syntactically wrong, rather than returning wrong data. Can anyone point me in the right direction? I'm using MySql if that makes a difference.

Outer joins have always confused me a bit!

[ May 07, 2007: Message edited by: Andy Westley ]
Andy Westley

Joined: Nov 09, 2004
Posts: 19
I've had another go:

LEFT JOIN (`spied`, `catalog_item` as catalog_item2, `user`) ON ((`spied`.`catalog_item_id` = `catalog_item2`.`id`) AND (`spied`.`user_id` = `user`.`id`) AND (`user`.`id` = 3))

WHERE`catalog_item`.`catalog_id` = `catalog`.`id`
AND`catalog`.`id` = 1
ORDER BY `catalog_item`.`name`

This gets me a bit closer, but the results are still not quite there:

"3";"Barn owl";"2007-05-01 09:27:49"
"3";"Barn owl";"2007-05-06 12:30:30"
"1";"Oyster catcher";"2007-05-01 09:27:49"
"1";"Oyster catcher";"2007-05-06 12:30:30"
"2";"Ringed plover";"2007-05-01 09:27:49"
"2";"Ringed plover";"2007-05-06 12:30:30"
"4";"Tawny owl";"2007-05-01 09:27:49"
"4";"Tawny owl";"2007-05-06 12:30:30"

The actual data says that Oyster catcher was spotted at 0927 and Barn owl was spotted at 1230.

Does my valiant attempt inspire anyone with some tips?
Andy Westley

Joined: Nov 09, 2004
Posts: 19
Bit of a brain burp, including the catalog_item table twice in there...

SELECT `catalog_item`.`id`,
FROM `catalog`,
LEFT JOIN (`spied`, `user`) ON ((`spied`.`catalog_item_id` = `catalog_item`.`id`) AND (`spied`.`user_id` = `user`.`id`) AND (`user`.`id` = 3))

WHERE `catalog_item`.`catalog_id` = `catalog`.`id`
AND `catalog`.`id` = 1
ORDER BY `catalog_item`.`name`

works fine.
I agree. Here's the link:
subject: Outer join headache
jQuery in Action, 3rd edition