douguabu8960 2016-02-09 21:38
浏览 203
已采纳

mySQL确定结果数组的第一个元素是否等于给定值

here's my table msg_to_user and imagine it's a chat app or a simple forum website

|msg_id|sender_id|receiver_id| subject_id |msg           |time               |
--------------------------------------------------------------------------
|   1  |  123    |  456      |     3      |"yadda yadd.."|2016-01-31 00:27:16|
|   2  |  456    |  123      |     3      |"ladida   .." |2016-01-31 00:37:16|
|   3  |  456    |  123      |     3      |"johndo   .." |2016-01-31 01:47:04|
|   4  |  123    |  456      |     3      |"xxxxxx   .." |2016-01-31 02:47:04|
|   5  |  456    |  123      |     3      |"qwerty   .." |2016-01-31 03:47:04|
|   6  |  789    |  456      |     9      |"dadda kadd.."|2016-01-31 00:11:16|
|   7  |  789    |  456      |     9      |"fadda jadd.."|2016-01-31 00:12:16|
|   8  |  123    |  789      |     9      |"fadda jadd.."|2016-01-31 00:13:16|

below is mysql query, that i use to determine how many, among all the threads i participated in a forum, are the one I actually started:

SELECT *
FROM msg_to_user 
GROUP BY subject_id // a given conversation unique id
ORDER BY time ASC  

Then i would use a PHP for-loop to check in every mtu_thread who is the first sender.

For instance for the conversation with subject_id = 3 you can see that user 123 actually started the thread

my question is: whether it's possible to just use MYSQL without using PHP

  • 写回答

3条回答 默认 最新

  • dsv73806 2016-02-09 21:51
    关注

    what about:

        SELECT x.*
        FROM ( 
            SELECT * 
            FROM msg_to_user 
            GROUP BY subject_id 
            ORDER BY time ASC ) AS x
        WHERE x.sender_id = 123
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?