468,291 Members | 1,491 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,291 developers. It's quick & easy.

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_Songs"
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|FavSongs|

|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_Songs"
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_Songs"
table, or to search all of the rows in the "TBL_All_Albums" table for
matches to one row in the "TBL_Fav_Songs" table.

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

Thanks,

Patrick
Jul 21 '08 #1
5 1124
Hi Patrick,

On Jul 21, 10:29 pm, Patrick A <park...@stradley.comwrote:
I have a table with 8,000 rows. Let's call that the "TBL_Fav_Songs"
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|FavSongs|

|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|IsFav|

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
Jul 21 '08 #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 ***
Jul 21 '08 #3
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
Jul 22 '08 #4
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
Jul 22 '08 #5
On Jul 22, 2:37 pm, Patrick A <park...@stradley.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
Jul 22 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Justin Emlay | last post: by
1 post views Thread by adeelanjum2001 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.