tags:

views:

21

answers:

4

I have this tables in my mysql schema:

posts:

   ---------------------------------------------
   id|type | userid | title | msg | time_added

users:

   ------------------
   userid | username

How can I show only 6 entries where: type is audio or video and only for a certain username?

A: 
Select title, msg, type
From Users Left Join Posts on Users.userid = Posts.userid
where type in ("audio", "video")
    and username = @username
limit 6
Andrew Cooper
Is there a reason for starting with a capital, except with 'where', 'in' and 'limit'?
Alec
No. Just me being inconsistent.
Andrew Cooper
A: 

Here's one way:

SELECT TOP 6 
      P.id
     ,P.type
     ,P.userid
     ,U.username
     ,P.title
     ,P.msg
     ,P.time_added
 FROM posts P
   INNER JOIN users U ON U.userid = P.userid
 WHERE U.username = 'given user name'
    AND (P.type = 'audio' OR P.type = 'video')
Miky Dinescu
+2  A: 
SELECT TOP 6 *
FROM posts
INNER JOIN users ON posts.userid = users.userid
WHERE type IN ('audio','video')
AND username = @username;

or (since TOP may be MS SQL Server only...)

SELECT *
FROM posts
INNER JOIN users ON posts.userid = users.userid
WHERE type IN ('audio','video')
AND username = @username
LIMIT 6;
djacobson
A: 
SELECT *
FROM posts p
INNER JOIN users u
  ON u.userid = p.userid
  AND u.username = 'name'
WHERE p.type IN ('audio','video')
ORDER BY time_added DESC
LIMIT 6
Alec