954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MS SQL Joins - newb assistance

I'm trying to get the following query both functional and controlled via inner joins rather than this format, as the way it is now will be slow once the db fills up

I've never been good with joins and this is the first time for me working with MS SQL rather than MySQL

help?

SELECT top 5
[user_friend].user_friendID as friendID,
[images].name as friendImage,
[user_bulletins].id as bulletin_id
[user_bulletins].title as bulletin_title
FROM
[user_bulletins], [user_friend], [images]
WHERE
[user_friend].user_id = [user_bulletins].user_id
AND
[images].user_id = [user_friend].user_friendID
AND
([user_friend].user_id = 64)
OR
([user_friend].user_friendID = 64)
AND
[images].display_photo = 1

lazzerous
Newbie Poster
1 post since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

may be typos here but you should try not to use square brackets in your sql; makes it much harder to read

also it is easier to join tables in the from clause rather than where, it makes more sense. I'd look in the documentation for more values but something like this may be easier :

SELECT top 5
uf.user_friendID as friendID,
i.name as friendImage,
ub.id as bulletin_id,
ub.title as bulletin_title
FROM
user_bulletins ub, 
join user_friend uf on ub.user_id = ub.user_id, 
join images i on ub.user_id = i.user_id
AND
(uf.user_id = 64 or uf.user_friendID = 64)
AND
i.display_photo = 1
pty
Posting Pro
530 posts since Oct 2005
Reputation Points: 64
Solved Threads: 39
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You