I'm trying to write a query to fetch some data. It's for a small script to display members that other members have 'saved' or deemed as favorites. The query is to select all of those favorites for a particular user.
A join is needed. The table that holds all of the favorites simply keeps the unique ID of the member, and the unique id of the member that is saved as a favorite. If a user has 20 favorites, there would be 20 records, each having the same 'member id', but different 'favorite member id's".
The first part of the query needs to select all records from the table 'member favorites' where 'member_id' = the member id of the person for whom we are displaying the favorites. So if i'm logged in and my member_id = 345 the query should look like
------------------------------
select *
FROM member_favorites
Where member_id = '345'
-----------------------------------
which might return say 5 records like
member_id = 345
favorite_member_id = 349
member_id = 345
favorite_member_id = 365
member_id = 345
favorite_member_id = 333
member_id = 345
favorite_member_id = 215
member_id = 345
favorite_member_id = 658
Now, that only gives me the unique ID for all of the members that have been saved. I still need to use all of those saved member id's to then query the core member table to get each member's username. But I'm not sure how to write the join to get both the ID's and the usernames. Can someone help me here?
The table structure:
(table) members
member_id
member_user_name
(table) member_favorites
member_id
favorite_member_id
A join is needed. The table that holds all of the favorites simply keeps the unique ID of the member, and the unique id of the member that is saved as a favorite. If a user has 20 favorites, there would be 20 records, each having the same 'member id', but different 'favorite member id's".
The first part of the query needs to select all records from the table 'member favorites' where 'member_id' = the member id of the person for whom we are displaying the favorites. So if i'm logged in and my member_id = 345 the query should look like
------------------------------
select *
FROM member_favorites
Where member_id = '345'
-----------------------------------
which might return say 5 records like
member_id = 345
favorite_member_id = 349
member_id = 345
favorite_member_id = 365
member_id = 345
favorite_member_id = 333
member_id = 345
favorite_member_id = 215
member_id = 345
favorite_member_id = 658
Now, that only gives me the unique ID for all of the members that have been saved. I still need to use all of those saved member id's to then query the core member table to get each member's username. But I'm not sure how to write the join to get both the ID's and the usernames. Can someone help me here?
The table structure:
(table) members
member_id
member_user_name
(table) member_favorites
member_id
favorite_member_id
