I am working on php-mysql script and having two tables namely messages and members. The data in these tables is given below for example:
messages table:
_________________________________________________________________
|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 |
------------------------------------------------------------------
and members table:
______________________________________________________________
| 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 |
---------------------------------------------------------------
Now I want these tables are joined in mysql query in such a way (or use two select in mysql) so that i should get the username, f_name, l_name, city and gender data of for every from_id and to_id. And also I should also get msg_subject,msg_body and sent_time. I am getting this with use of another mysql query in while loop, which I dont want to use because of performance issue for more than 500 rows. Finally I want display the following type of output using html in the web browser:
_____________________________________________________________________________
|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 | | | |
-----------------------------------------------------------------------------
and so on... for m_id row no. 3 also..
I am getting this html table style output as shown above using another mysql query in while loop but i want to achieve this in a single mysql query using double select or two joins. Please suggest me the way and give me that mysql query details.... thanks in advance.. (also to disclose I am just a starter in php-mysql programming..)
Example of using 2 left joins for "from member" and "to member"
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.