php - How to retrieve a chat msg with sender and receiver username in mysql -
chat table ------------------------------------------------------- msg_id msg(description of msg) sender(id) receiver(id) ------------------------------------------------------- 1 hello 2 1 1 how 1 2 ---------------------------------------------------------- user table ----------------------- u_id username ----------------------- 1 usman 2 ali ---------------------
here see 2 tables 1 chat , 1 table user make relationship between these 2 table feel difficulty how extract data these table.
what want..
i want extract each sender msg username , each reciver name username facebook chat make query
i put query in database
select u.user_name username,msg.msg user u,messages msg (msg.sender or msg.receiver)=(select u_id user u_id= 1 ) , msg.sender=1 order msg.msg_id desc
but query repeat sender message receive name , receiver msg sender name
i want show sender , reciver msg show separably name tell me method how fix it.
what have never work. or
comparison return boolean true/false value, compare against value of u_id
. , since u_id
query hardcoded return 1
, you're doing where true/false = 1
.
you need join user
table twice (once receiver, once sender):
select sender.user_name, receiver.user_name, msg.msg msg left join user sender on msg.sender_id = sender.u_id left join user receiver on msg.receiver_id = receiver.u_id order msg.msg_id