Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Simple JOIN in Access 2002

Simple JOIN in Access 2002

Scheduled Pinned Locked Moved Database
databasehelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Larsenal
    wrote on last edited by
    #1

    I have three tables: - Article - Album - linkArticleAlbum I want get a list of all the albums and also have some way of determining whether it is linked to the currently selected article. What I WANT to do is something like

    SELECT
    linkArticleAlbum.Article,
    Album.ID,
    Album.Title
    FROM Album LEFT JOIN linkArticleAlbum ON linkArticleAlbum.Album = Album.ID
    WHERE linkArticleAlbum.Article = 7 OR linkArticleAlbum.Article IS NULL

    Here's what my sample data looks like

    Album

    | ID |
    1
    2
    3

    Article

    | ID |
    7
    8
    9

    linkArticleAlbum

    | Article | Album
    7 1
    8 1
    9 2

    As you can see, I won't get a record with Album 2 since my WHERE clause eliminates it. I'm stuck. Advise anything, including a redesign of my DB. I've apparently done something stupid and my murky brain can't figure it out right now. ········ to die is gain ········

    T 1 Reply Last reply
    0
    • L Larsenal

      I have three tables: - Article - Album - linkArticleAlbum I want get a list of all the albums and also have some way of determining whether it is linked to the currently selected article. What I WANT to do is something like

      SELECT
      linkArticleAlbum.Article,
      Album.ID,
      Album.Title
      FROM Album LEFT JOIN linkArticleAlbum ON linkArticleAlbum.Album = Album.ID
      WHERE linkArticleAlbum.Article = 7 OR linkArticleAlbum.Article IS NULL

      Here's what my sample data looks like

      Album

      | ID |
      1
      2
      3

      Article

      | ID |
      7
      8
      9

      linkArticleAlbum

      | Article | Album
      7 1
      8 1
      9 2

      As you can see, I won't get a record with Album 2 since my WHERE clause eliminates it. I'm stuck. Advise anything, including a redesign of my DB. I've apparently done something stupid and my murky brain can't figure it out right now. ········ to die is gain ········

      T Offline
      T Offline
      turbochimp
      wrote on last edited by
      #2

      Well, you haven't done anything clearly wrong in your database design, but your query needs some help. If you include a WHERE clause, only the rows that satisfy the condition will be listed, so LEFT JOINing won't help. If it's really important to have the results indicate whether the album is linked to the selected article, this would probably do the trick: SELECT al.ID, al.Title, l.Article, IIf([l].[Article] Is Not Null AND l.Article = [article ID],"Linked","Unlinked") AS IsLinked FROM Album AS al LEFT JOIN linkArticleAlbum AS l ON al.ID = l.Album; Given your setup, where a single Album may be associated with many Articles, you should be aware that you may get multiple results for a single Album (from your example, Album 1 would appear twice in the results). If you don't care about indicating in the results whether the Album is linked (i.e. you want to handle that in code) then the query is even shorter: SELECT al.ID, al.Title, l.Article FROM Album AS al LEFT JOIN linkArticleAlbum AS l ON al.ID = l.Album; Hope this helps.

      The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

      L 1 Reply Last reply
      0
      • T turbochimp

        Well, you haven't done anything clearly wrong in your database design, but your query needs some help. If you include a WHERE clause, only the rows that satisfy the condition will be listed, so LEFT JOINing won't help. If it's really important to have the results indicate whether the album is linked to the selected article, this would probably do the trick: SELECT al.ID, al.Title, l.Article, IIf([l].[Article] Is Not Null AND l.Article = [article ID],"Linked","Unlinked") AS IsLinked FROM Album AS al LEFT JOIN linkArticleAlbum AS l ON al.ID = l.Album; Given your setup, where a single Album may be associated with many Articles, you should be aware that you may get multiple results for a single Album (from your example, Album 1 would appear twice in the results). If you don't care about indicating in the results whether the Album is linked (i.e. you want to handle that in code) then the query is even shorter: SELECT al.ID, al.Title, l.Article FROM Album AS al LEFT JOIN linkArticleAlbum AS l ON al.ID = l.Album; Hope this helps.

        The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

        L Offline
        L Offline
        Larsenal
        wrote on last edited by
        #3

        Thanks for the help. The example does help to some degree, but in one iteration of my attempts I achieved essentially the same thing. To clarify, I would prefer to have each Album appear only once. With the current queries, each album shows up at least once, but will appear as many times as it is referenced in the linkArticleAlbum table. Can I eliminate the duplicates while ensuring that I can test for l.Article = [article ID]???

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups