Jump to content

get top 1 from each where clause


Crusader

Recommended Posts

This is a table called "tagged"

 

id 	post_id 	tag_id
1 	206 		6
2	206 		8
3 	206 		4
4 	206 		3
5 	206 		2
6 	205 		3
7 	205 		8
8 	205 		2
9 	204 		4
10 	204 		2
11 	203 		3
12 	203 		2
13 	202 		4
14 	202 		2

 

How would i get the first tag_id for each post_id?

 

I've tried something like this but I don't know where to go from there...

 

SELECT * FROM `tagged` WHERE post_id IN(202,206)

 

Thanks

Link to comment
Share on other sites

 

SELECT 
           t.* 
  FROM 
           (SELECT
                        *
               FROM
                        `tagged` 
             ORDER BY
                        post_id ASC, tag_id ASC
           ) AS t

  GROUP BY
            t.post_id
;

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.