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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?