Random Select Query

  • OK so let's take a bit further with the assumption of a Users table.

    DECLARE @CategoryID INT -- SHOULD BE A PARAMETER

    SET @CategoryID = 1

    SELECT

     TOP 5

     JokeID,

     JokeCatID,

     JokeTitle,

     JokeDesc

     UserID

    FROM

     dbo.Jokes C

    INNER JOIN

     (SELECT

       --UserID, -- Uncomment if JokeID is not a unique key value

       (SELECT TOP 1 JokeID FROM dbo.Jokes B WHERE A.UserID = B.UserID AND B.JokeCatID = @CategoryID ORDER BY NEWID()) JokeID

      FROM

        dbo.Users A) D

    ON

     C.JokeID = D.JokeID

     -- AND C.UserID = D.UserID -- Uncomment if JokeID is not a unique key value

    ORDER BY

     NEWID()

    And base on what you are after you might not even need anything but Title an Description so make sure only to return what you need.

    And this is why we are a community. Lots of eyes can make a huge difference.

  • Yup, so where did you say I had to send that resume?

Viewing 2 posts - 16 through 16 (of 16 total)

You must be logged in to reply to this topic. Login to reply