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
https://forums.phpfreaks.com/topic/86097-get-top-1-from-each-where-clause/
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
;

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.