duanou9739 2015-11-23 18:48
浏览 58

是否可以将这两个SQL语句合并为一个?


I am using these two MySQL statements to get all messages between two users, but I am running into a pretty big problem when it comes to manipulating the data later in the code since it's not in order by ID. Is there any way to combine these statements AND order the results by ID?

$sql = "SELECT * FROM messages WHERE sender='" . $username . "' AND receiver='" . $chatPartner . "'"

$sqlTwo = "SELECT * FROM messages WHERE sender='" . $chatPartner . "' AND receiver='" . $username . "'"

Essentially, I need every occurrence of a message between two people, but sorted by ID. As of right now, I am joining the arrays to get my full list, but it's not in order by ID.

  • 写回答

3条回答 默认 最新

  • dpxpz37157 2015-11-23 18:51
    关注
    SELECT * FROM messages 
    WHERE (sender='" . $username . "' AND receiver='" . $chatPartner . "'")
    OR (sender='" . $chatPartner . "' AND receiver='" . $username . "'")
    ORDER BY id DESC
    
    评论

报告相同问题?

悬赏问题

  • ¥100 探针系统研发相关实习报告编写问题
  • ¥15 求帮生成一个lattice diamond的许可证
  • ¥15 大一前端新生求教学解答
  • ¥15 如何制作一个可以查看“网游有序列的装备词条”的软件/插件
  • ¥15 CS2打5E与完美天梯匹配会与服务器断开连接(黑框没标明具体原因)
  • ¥15 利用cst反推材料电磁参数,推出想x,y,z方向的相对介电常数与磁导率
  • ¥15 求帮助!用赛灵思FPGA XC7A35T对一个频率50MHz的数字信号读取高低电平,只用HR bank普通单端io进行采样可以吗
  • ¥15 训练准确率100%,测试准确率只有50%
  • ¥15 grafana创建dashhabord提示no data sources of type Prometheus Alert
  • ¥15 python用arima时间序列法预测不出结果 急