PDA

View Full Version : Show top 10


ooiyh
10-12-2009, 08:05 AM
Hi,

I would like to show the top 10 tuples from my querys, doesn't seems to work. I am using ms sqlserver
Please help.:)


SELECT TOP 10 * FROM
(
SELECT Fbuser_id_friend FROM BEFRIEND WHERE Fbuser_id --Select the friends of the user friends
IN(SELECT Fbuser_id_friend FROM BEFRIEND WHERE Fbuser_id = '1000')
AND Fbuser_id_friend <> '1000'
EXCEPT
SELECT Fbuser_id_friend FROM BEFRIEND
WHERE Fbuser_id = '1000'
)

abduraooft
10-12-2009, 08:19 AM
I am using ms sqlserver and posted in MySQL forum?
Request a moderator to move your thread to "other databases (http://www.codingforums.com/forumdisplay.php?f=38)" section

Old Pedant
10-12-2009, 07:22 PM
Ummm...you are doing SELECT *, but the only field you can possibly get from that * is the one and only field SELECTed by the inner query.

Maybe if you explained:
(1) Your table layout; what fields are where and what they mean
and
(2) What you want as your final result.

It's hard to figure out what you are after when all we can see is some code that obviously isn't working.

ooiyh
10-13-2009, 06:14 AM
I got my final result, which is the code in (). But I just want to show only 10 out of my final result. How do I do it?

Cheers:)

Old Pedant
10-13-2009, 07:21 AM
As I said, I wish you had EXPLAINED the problem instead of showing code that might work but might not be best.

But since you insist:

SELECT TOP 10 B.* FROM
(
SELECT Fbuser_id_friend FROM BEFRIEND WHERE Fbuser_id --Select the friends of the user friends
IN(SELECT Fbuser_id_friend FROM BEFRIEND WHERE Fbuser_id = '1000')
AND Fbuser_id_friend <> '1000'
EXCEPT
SELECT Fbuser_id_friend FROM BEFRIEND
WHERE Fbuser_id = '1000'
) AS S, BEFRIEND AS B
WHERE B.Fbuser_id_friend = S.Fbuser_id_friend
ORDER BY ???what???

Selecting a TOP 10 is meaningless if you don't give SQL something to order by and thus find the top 10 whatevers.