How to Compare All Rows of TBL_1 All Rows of TBL_2?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick A

    How to Compare All Rows of TBL_1 All Rows of TBL_2?

    All,

    I need a little help understanding which concepts / functions /
    methods / strategies I should use to accomplish what I'll describe
    below. Note - the real implementation isn't about music - I just
    thought it would be easier to understand, if not more fun to think
    about.

    I have a table with 8,000 rows. Let's call that the "TBL_Fav_So ngs"
    table. It contains a list of my favorite songs, in the column |
    MyFavs|.

    I have a table with 117,000 rows. Let's call that the
    "TBL_All_Albums " table. It contains a list of albums in my music
    library. The table has three columns - |Album|Songs|Fa vSongs|

    |Album| contains the Album name.
    |Songs| contains the songs on an album, in a text field - all songs
    listed together in one field.
    |FavSongs is empty at the moment.

    I want to wind up with a table in which FavSongs is populated with the
    names of songs that are my favorites, for each album listed.

    I need to search the |Songs| field of the "Albums" table, for matches
    from the songs listed in the |MyFavs| column in the "TBL_Fav_So ngs"
    table.

    Wherever I find a match, I want to populate the |FavSongs| field.
    There can be multiple matches per album, and a song can be on multiple
    albums ($%^& annviersary reissues!).

    I think I'm going to need to use nested loops, and have a routine that
    fills in the |FavSongs| field, appending new values to the field as it
    finds matches. I guess I don't know whether to search one row in the
    "TBL_All_Albums " table for matches to every row in the "TBL_Fav_So ngs"
    table, or to search all of the rows in the "TBL_All_Albums " table for
    matches to one row in the "TBL_Fav_So ngs" table.

    Suggestions, including a good resource for learning how to do this?

    Thanks,

    Patrick
  • cjakeman

    #2
    Re: How to Compare All Rows of TBL_1 All Rows of TBL_2?

    Hi Patrick,

    On Jul 21, 10:29 pm, Patrick A <park...@stradl ey.comwrote:
    I have a table with 8,000 rows. Let's call that the "TBL_Fav_So ngs"
    table. It contains a list of my favorite songs, in the column |
    MyFavs|.
    >
    I have a table with 117,000 rows. Let's call that the
    "TBL_All_Albums " table. It contains a list of albums in my music
    library. The table has three columns - |Album|Songs|Fa vSongs|
    >
    |Album| contains the Album name.
    |Songs| contains the songs on an album, in a text field - all songs
    listed together in one field.
    |FavSongs is empty at the moment.
    >
    I want to wind up with a table in which FavSongs is populated with the
    names of songs that are my favorites, for each album listed.
    I would copy TBL_All_Albums into a table with a different structure
    such as
    TBL_All_Albums_ Revised with three columns - |Album|Song|IsF av|

    Then I could set the IsFav flag by running a simple SQL query such as
    UPDATE TBL_All_Albums_ Revised
    SET IsFav = TRUE
    WHERE Song = ANY(
    SELECT MyFavs
    FROM TBL_Fav_Songs);

    For a database to work well, each entry in column Song should contain,
    not a list of songs, but a single song.
    Hope that helps,

    Chris

    Comment

    • Rich P

      #3
      Re: How to Compare All Rows of TBL_1 All Rows of TBL_2?

      Try something like the following query which would update TBL_All_Albums
      by setting the FavSongs column equal to the FavSongs column in
      TBL_Fav_Songs. So for every row that contains a song in the Song column
      which matches a FavSong, the first FavSong column would get the matching
      FavSong from the 2nd table.



      UPDATE TBL_All_Albums t1 INNER JOIN TBL_Fav_Songs t2 ON t1.Song =
      t2.FavSong SET t1.FavSong = t2.FavSong


      Rich

      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • cjakeman

        #4
        Re: How to Compare All Rows of TBL_1 All Rows of TBL_2?

        Hi Rich,
        So for every row that contains a song in the Song column
        which matches a FavSong, the first FavSong column would get the matching
        FavSong from the 2nd table.
        >
        UPDATE TBL_All_Albums t1 INNER JOIN TBL_Fav_Songs t2 ON t1.Song =
        t2.FavSong SET t1.FavSong = t2.FavSong
        Your SQL is fine but Patrick A writes:
        "|Songs| contains the songs on an album, in a text field - all songs
        listed together in one field."
        so the SQL will result in zero updates.

        Once he gets the structure right, your SQL will do the heavy lifting
        for him.

        Just my thoughts,

        Chris

        Comment

        • Patrick A

          #5
          Re: How to Compare All Rows of TBL_1 All Rows of TBL_2?

          Rich, Chris,

          Thanks for trying to help.

          Two issues:

          1. I cannot change the structure of the database.
          2. Knowing if an album has a favorite is not sufficient. The result I
          need is to have the favorite (or favorites - there can be multiples)
          written to the |FavSongs| field in the TBL_All_Albums.

          I'll keep digging...

          Patrick

          Comment

          • cjakeman

            #6
            Re: How to Compare All Rows of TBL_1 All Rows of TBL_2?

            On Jul 22, 2:37 pm, Patrick A <park...@stradl ey.comwrote:
            Two issues:
            >
            1. I cannot change the structure of the database.
            2. Knowing if an album has a favorite is not sufficient. The result I
            need is to have the favorite (or favorites - there can be multiples)
            written to the |FavSongs| field in the TBL_All_Albums.
            In that case, you will need to use some VBA code and, as you
            predicted, nested loops. I'm sure we can help with that.

            I would scan the larger table and compare each song with the list of
            favourites. How are you separating each of the songs in the Songs
            field?

            Chris

            Comment

            Working...