简体   繁体   English

mySQL查询在相同条件下检索数据的方式有所不同

[英]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 MessagesTo 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM