可以使用RIGHT或LEFT有效地使用Inner join在单个mysql查询中加入

时间:2017-11-14 08:04:44

标签: php mysql sql inner-join

我正在研究php-mysql脚本,并有两个表,即消息和成员。这些表中的数据如下所示:

消息表:

_________________________________________________________________
|m_id| from_id | to_id | msg_subject | msg_body     | sent_time  |
------------------------------------------------------------------
| 1  |    142  |  335  |    hello    |  how are you | 2017-25-10 |
------------------------------------------------------------------
| 2  |    225  |  443  |    hi       |  whats up..  | 2017-26-10 |
------------------------------------------------------------------
| 3  |    332  |  110  |    urgent   |  call me now | 2017-27-10 |
------------------------------------------------------------------

和成员表:

______________________________________________________________
| id   | username | f_name  |  l_name   |   city    | gender  |
---------------------------------------------------------------
| 110  |    joe   |  Joe    |    Tom    |  Rome     |    M    |
---------------------------------------------------------------
| 142  |    alb_p |  Albert |    Paul   |  Milan    |    M    |
---------------------------------------------------------------
| 225  |    ruby  |  Ruby   |    Hinge  |  Paris    |    F    |
---------------------------------------------------------------
| 332  |    ram   |  Ram    |    Sharma |  Delhi    |    M    |
---------------------------------------------------------------
| 335  |   kris   |  Kity   |    Krin   |  Milan    |    F    |
---------------------------------------------------------------
| 443  |    reema |  Reema  |    Pink   |  Dubai    |    F    |
---------------------------------------------------------------

现在我希望这些表以这种方式加入mysql查询(或者在mysql中使用两个select),这样我就可以得到每个from_id和to_id的用户名,f_name,l_name,城市和性别数据。而且我还应该得到msg_subject,msg_body和sent_time。我在while循环中使用另一个mysql查询得到这个,我不想使用,因为超过500行的性能问题。最后,我想在Web浏览器中使用html显示以下类型的输出:

_____________________________________________________________________________
|m_id|  Message       |   Message      | Message  |   Message    | Msg Sent |
|    | From Member    |   To Member    | Subject  |   Details    |  On Date |    
-----------------------------------------------------------------------------
|    |ID: 142         | ID: 335        |          |              |          |
|    |UserName: alb_p | UserName: kris |          |              |          |
|    |1st Name:Albert | 1st Name: Kity |          |              |          |
| 1  |Last Name: Paul | Last Name: Krin|  hello   | how are you  |2017-25-10|
|    |City: Milan     | City: Milan    |          |              |          |
|    |Gender: Male    | Gender: Female |          |              |          |
-----------------------------------------------------------------------------
|    |ID: 225         | ID: 443        |          |              |          |
|    |UserName: ruby  | UserName:reema |          |              |          |
|    |1st Name:Ruby   | 1st Name:Reema |          |              |          |
| 2  |Last Name:Hinge | Last Name:Pink |  hi      | Whats up..   |2017-26-10|
|    |City: Paris     | City: Dubai    |          |              |          |
|    |Gender: Female  | Gender: Female |          |              |          |
-----------------------------------------------------------------------------

依此类推......对于m_id行号。 3也.. ..

我正在使用while循环中的另一个mysql查询获取此html表样式输出,但我想在使用双精度或两个连接的单个mysql查询中实现此目的。请建议我的方式,并给我mysql查询详细信息....提前感谢..(也透露我只是php-mysql编程的首发..)

1 个答案:

答案 0 :(得分:0)

使用2个左连接的示例"来自成员"和"成员"

select
       m.*, from_men.username, to_mem.username
from messages m
left join members as from_mem on m.from_id = from_mem.id
left join members as to_mem   on m.to_id = to_mem.id