• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Outer join headache

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
UNION
(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!

Cheers
[ May 07, 2007: Message edited by: Andy Westley ]
 
Andy Westley
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've had another go:

SELECT`catalog_item`.`id`,
`catalog_item`.`name`,
`spied`.`spied`
FROM`catalog`,
`catalog_item`
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
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bit of a brain burp, including the catalog_item table twice in there...

SELECT `catalog_item`.`id`,
`catalog_item`.`name`,
`spied`.`spied`
FROM `catalog`,
`catalog_item`
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic