duanqiao2225 2013-10-20 18:20
浏览 53
已采纳

如何使用SQL每个值限制项目的选择? [重复]

This question already has an answer here:

Ok, so I have this private messaging system I'm working on, and here's kind of how it's organized

ID From-ID To-ID Message Thread
1 0 1 "blah blah blah" 1
2 0 1 "blah blah blah, blah!" 1
3 0 3 "hey <3>, how are you? Signed, <0>" 2

The way it's like this is so that you can have multiple messages in a thread, so you can reply.

Then on the left side of my private messages page, you can select which thread to look at.

So, it'll be a button that says the sender's name, so I used this SQL:

mysql_query("SELECT * FROM pms WHERE to-id='".$_SESSION['id']."' ORDER BY id ASC");

But, I want it to be something like (pseudo-code)

mysql_query("SELECT * FROM pms WHERE to-id='".$_SESSION['id']."' LIMIT 1 per `thread` ORDER BY id ASC");

So that it will only show one link to a thread.

What's happening now though is that it will show a new button, every time there is a reply to the thread. So, if there are 100 replies to message, and they're all in the same thread (since they're "replies") there will be 101 buttons, which is not ideal, for obvious reasons.

Everything is currently working, except for that button limitation that I'm talking about.

</div>
  • 写回答

1条回答 默认 最新

  • duandaoji3992 2013-10-20 18:29
    关注

    First things first... your ID column... shouldn't it be auto-incrementing? Just checking...

    If you want to get all the threads, you might want to do something like...

    Select distinct thread from pms
    
    -- then optionally...
    where to-id = '".$_SESSION['id']"' order by ID asc
    

    That way, you'll just get the first instance of that each thread.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)