MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 22nd, 2009, 12:30 PM
techboyjk techboyjk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Location: St. Louis
Posts: 93 techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level)techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level)techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level)techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level)techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level)techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 10 m 23 sec
Reputation Power: 22
Need help writing join query...

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

Reply With Quote
  #2  
Old June 22nd, 2009, 01:04 PM
techboyjk techboyjk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Location: St. Louis
Posts: 93 techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level)techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level)techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level)techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level)techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level)techboyjk User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 10 m 23 sec
Reputation Power: 22
How does this look? It uses two joins though. Not sure if this is the most efficient way.

select MemFav.*, M1.UserName as MemberUserName, M2.UserName As FavoriteMemberUserName

FROM member_favorites MemFav INNER JOIN Members M1 On MemFav.Member_ID = M1.Member_ID INNER JOIN Members M2 On MemFav.Favorite_Member_ID = M2.Member_ID

Where MemFav.member_id = '345'
Comments on this post
jzd agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Need help writing join query...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
Stay green...Green IT