根据mysql中的两个组合列选择唯一元组

时间:2013-10-02 16:47:50

标签: mysql

我有一张桌子:

|------------|---------------|----------------|---------------|
|    id      |  social_media |   post_userid  |    value      |
|-------------------------------------------------------------|
|     1      |    facebook   |    1000012     |     12        |
|------------|---------------|----------------|---------------|
|     2      |    facebook   |    1000012     |     14        |
|-------------------------------------------------------------|
|     3      |    twitter    |    10023       |     12        |
|-------------------------------------------------------------|
|     4      |    facebook   |    1000014     |     12        |
|-------------------------------------------------------------|
|     5      |  google plus  |      123       |     14        |
|-------------------------------------------------------------|

在表格中2 facebook users having id 1000012 and 1000014 one twitter user having id 10023one googleplus user having id 123

因此,social_mediapost_userid组合可识别唯一身份用户。

那么如何根据social_mediapost_userid的组合获得不同的用户?

2 个答案:

答案 0 :(得分:1)

SELECT DISTINCT social_media, post_userid FROM my_table

答案 1 :(得分:-1)

 Select id ,social_media ,   post_userid  ,    value
 From table t1
 Where id = (Select Max(id) from table t2
               where t1.post_userid = t.post_userid)