用选定的条件php mysql连接两个表

时间:2014-09-10 05:19:30

标签: php mysql

我在数据库和查询中使用了三个表,我应该结合使用。表格如下:

表格消息:

message_id   | msg_id |   conv_id   | text    | user_id
--------------------------------------------------------------
    1        |    1   |     1       |  hi     |   12
    2        |    1   |     1       |  Thanks |   14
    3        |    2   |     4       |  helo   |   21

表格对话:

   conv_id      |   msg_id |   conv_id   | subject  | user_id  | text 
------------------------------------------------------------------------
       1        |   1      |     1       |  sub1    |    12    |  Hi to all
       2        |   1      |     1       |  sub1    |    14    |  Hi to all
       3        |   2      |     4       |  sub3    |    21    |  reply2

具有msg_id的两个表是常见的,

我的预期结果是,

来自会话表组的msg_id,

来自messsage表message.msg_id = conversation.msg_id

   conv_id      |   msg_id   |   conv_id   | subject  | text
-----------------------------------------------------------------
       1        |   1        |     1       |  sub1    | Hi to all
       1        |   1        |     1       |  sub1    | hi
       2        |   1        |     1       |  sub1    | thanks
       3        |   2        |     4       |  sub3    | helo

我的查询是

SELECT *
FROM mailbox_message m1
LEFT JOIN mailbox_conversation m2 ON m1.conversation_id=m2.conversation_id
WHERE m2.msg_id='1'
ORDER BY m1.created DESC

我的查询返回以下内容

   conv_id      |   msg_id   |   conv_id   | subject  | text
-----------------------------------------------------------------
       1        |   1        |     1       |  sub1    |   Hi to all
       2        |   1        |     1       |  sub1    |   Hi to all    
       1        |   1        |     1       |  sub1    |    hi
       2        |   1        |     1       |  sub1    |    thanks
       3        |   2        |     4       |  sub3    |    helo

0 个答案:

没有答案