需要查询帮助

发布于 2024-11-09 07:50:42 字数 275 浏览 0 评论 0原文

我有下表。它存储每个用户在聊天中发送和接收的消息。我想选择用户已发送和接收的所有消息并检索它们。然后我可以根据 JQUERY 中的对话 ID 对它们进行排序。我是否必须为此执行两个查询,或者我可以嵌入一个查询吗?本质上我想做的

SELECT * FROM chatbox WHERE sender=? 
and 
SELECT * FROM chatbox WHERE receiver=?

只是想尽量避免对结果进行两个查询和两个 while 循环。有人有什么建议吗?

I have the following table. It stores messages that each user has sent and received in chats. I want to select all the messages that a user has sent and received and retrieve them. I can then sort them by their conversation id in JQUERY. Would I have to do two queries for this or can I embed a query? Essentially what I want to do is

SELECT * FROM chatbox WHERE sender=? 
and 
SELECT * FROM chatbox WHERE receiver=?

I just want to try to avoid having two queries and two while loops for the results. Anyone have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌路终见情 2024-11-16 07:50:42

你可以这样做,也可以使用 MySQL 进行排序:

SELECT *
FROM chatbox
WHERE
  sender="<id>" OR
  receiver="<id>"
ORDER BY conversation_id

You can do it like this, and also do the ordering with MySQL too:

SELECT *
FROM chatbox
WHERE
  sender="<id>" OR
  receiver="<id>"
ORDER BY conversation_id
你如我软肋 2024-11-16 07:50:42

您可以在单个查询中完成此操作并进行排序;

SELECT * FROM chatbox WHERE 'username' IN (sender, receiver) ORDER BY id

You can do it in a single query along with the sorting;

SELECT * FROM chatbox WHERE 'username' IN (sender, receiver) ORDER BY id
羁绊已千年 2024-11-16 07:50:42
SELECT 
  *
FROM 
  chatbox
WHERE
  sender=? or receiver=?
SELECT 
  *
FROM 
  chatbox
WHERE
  sender=? or receiver=?
甜是你 2024-11-16 07:50:42
SELECT * FROM chatbox WHERE sender=? OR receiver=?
SELECT * FROM chatbox WHERE sender=? OR receiver=?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文