[英]mySQL query retrieves data differently in same condition
I am have made a chat application in Node-JS and Socket.io with MySQL database. 我已经在Node-JS和Socket.io中使用MySQL数据库制作了一个聊天应用程序。 I am done with everything but got stuck in one thing, that I cannot solve.
我已经完成了所有工作,但陷入一件事,我无法解决。 When a user login to their account, they are redirected to their inbox and then the list of user he chatted with are displayed.
当用户登录其帐户时,他们将被重定向到其收件箱,然后显示与他聊天的用户列表。 For example the current user who logged in is
John
and he chatted with a person name Doe
when John
clicks the Doe
name from the list to open the chats, at that time all the messages are retrieved from the database. 例如,当前登录的用户是
John
,当John
从列表中单击Doe
名称以打开聊天时,他与一个人名Doe
聊天,这时从数据库中检索了所有消息。 The data is stored as Array of Objects
in a variable data
. 数据作为
Array of Objects
存储在变量data
。 What I did in the code that, I have looped through this data
and appended each message to the chat box. 我在代码中所做的工作是遍历此
data
并将每条消息附加到聊天框。 Where all the messages displays. 显示所有消息的位置。 There are two categories in each message
Message To
and Message From
. 每个消息“
Message To
和“ Message From
有两个类别。 These categories are because to distinguish that who texted whom. 这些类别是因为要区分发短信给谁。 And then I change the
background color
of these messages. 然后,我更改这些消息的
background color
。 The messages sent from my side will have background Grey
and the person messaged me will have Blue
background. 从我这边发送的消息将具有
Grey
背景,而向我发送消息的人将具有Blue
背景。
Now the basic problem I faced is, when the length of From Messages
or To Messages
are less than 13 then the chat is displayed in a sequence and each of the messages are on the right place. 现在我所面临的基本问题是,当长度
From Messages
或To Messages
是小于13,则聊天显示在序列和每个消息都在正确的地方。 But when the length of any of the above messages exceeds 13 then what happens is, The messages I have sent are displayed first, and then the user who sent me the messages are displayed. 但是,当以上任何一条消息的长度超过13时,将发生以下情况:首先显示我已发送的消息,然后显示向我发送消息的用户。
Messages from and Messages to have length less than 13 so its fine now 来自和的邮件长度小于13,因此现在还可以
When it exceeds the length 13 then here first my messages are shown 当它超过长度13时,这里首先显示我的消息
And then the user sent to me are shown 然后显示了发送给我的用户
The data format 数据格式
The Script Displaying the chat 显示聊天的脚本
var ac_user = data.active_user; socket.on("get_ret_messages",(data)=>{ while (chatBody.hasChildNodes()) { chatBody.removeChild(chatBody.firstChild); } for(let i = 0; i<data.length;i++){ if(data[i].mess_to == ac_user){ mess = '<div class="bubble you">'+data[i].mess_txt+'<br><span class="time">'+data[i].mess_time+'</span></div>'; chatBody.insertAdjacentHTML("beforeend",mess); } else{ mess = '<div class="bubble me">'+data[i].mess_txt+'<br><span class="time">'+data[i].mess_time+'</span></div>'; chatBody.insertAdjacentHTML("beforeend",mess); } } chatBody.scrollTop = chatBody.scrollHeight; });
The Query 查询
socket.on("get_user_messages",(data)=>{ var GET_CLICKED_USER_MESS = "SELECT mess_txt,mess_time,mess_to FROM (select mess_to as user_id,mess_txt,mess_time,mess_id,mess_to from messages where mess_from = '"+data.active+"' AND mess_to='"+data.clicked+"' UNION select mess_to as user_id,mess_txt,mess_time,mess_id,mess_to from messages where mess_from = '"+data.clicked+"' AND mess_to='"+data.active+"' ORDER BY mess_id ASC) sq join users on users.id = sq.user_id"; con.query(GET_CLICKED_USER_MESS,(err,res)=>{ if(err) throw err; socket.emit("get_ret_messages",res); }) });
I am quite sure that the problem can be in the query, but I don't know what is that.
我非常确定问题可能出在查询中,但是我不知道那是什么。
For the little explanation: the ORDER BY
must be placed at the end for it to be effective, or else it will onlt order the sub-query, and the order was probably messed up by the JOIN
稍作解释:必须将
ORDER BY
放在最后才能使其生效,否则它将对子查询进行排序,并且该JOIN
可能被JOIN
弄乱了
the query: 查询:
"SELECT mess_txt,mess_time,mess_to FROM (SELECT mess_to AS user_id,mess_txt,mess_time,mess_id,mess_to FROM messages WHERE mess_from = '"+data.active+"' AND mess_to='"+data.clicked+"' UNION SELECT mess_to as user_id,mess_txt,mess_time,mess_id,mess_to FROM messages WHERE mess_from = '"+data.clicked+"' AND mess_to='"+data.active+"') sq JOIN users ON users.id = sq.user_id ORDER BY mess_time ASC"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.