dsa2c2255888 2014-09-18 21:02
浏览 26

mysql连接无法正常工作

I have 3 tables in mysql database

chat
user1, user2, chat_key

chat_seen
msgid, userid, viewed

users
username, status, key

So im trying to display the chats ordering them by the users online then messages seen. But its not working. this is what i have so far, iv only got it to work with chat and users, i dont know how to add order by chat seen

("SELECT u.random, u.status, c.chat_key, c.seen, u.username
                                     FROM chat c, users u
                                     WHERE CASE 
                                     WHEN c.user1 = ?
                                     THEN c.user2 = u.random
                                     WHEN c.user2 = ?
                                     THEN c.user1= u.random
                                     END 
                                     AND (
                                     c.user1 = ?
                                     OR c.user2 = ?)
                                     ORDER BY u.status DESC LIMIT 50");

So the results should be

username - online - (2 messages)
username - online - (1 message)

username - offline - (20 messages)
username - offline - (2 messages)
username - offline - (o messages)

At the moment i have

username - online - (0 messages)
username - online - (0 messages)
username - offline - (2messages)
username - offline - 20 messages)
  • 写回答

1条回答 默认 最新

  • dtvx3420 2014-09-18 22:11
    关注

    Try this query below:

    ("SELECT 
    u.random, 
    u.status, 
    c.chat_key, 
    c.seen, 
    u.username
    
    FROM chat c, users u
    
    WHERE 
    (c.user1 = ? AND c.user2 = u.random)
    OR
     (c.user2 = ? AND c.user1 = u.random)
    AND 
    (c.user1 = ? OR c.user2 = ?)
    
    ORDER BY u.status DESC LIMIT 50");
    

    Let me know if it works. :D

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭