有用户活动记录表activity和好友表mem_friends
现在要找出用户自己和好友的活动记录sql语句我是这样写,但show processlist 有时会显示sending data..,网页就会卡住 有时又不会 是不是sql语句有问题?select * from activity where author='$u_id' or author in (select client_name from mem_friends where main_name='$u_id' and client_name!='$u_id' and isok='yes')

解决方案 »

  1.   

    具体看看这个http://apps.hi.baidu.com/share/detail/38868431
      

  2.   

    首先查找好友id的信息,不应该在sql中写吧。如果好友为空,就不需要查数据库了哇。这样可以减少数据库的负担。所以你应该吧自己的id和朋友的id都传过去。然后用连接查询。
    select * from activity where author in (自己的id和friends的id);
      

  3.   

    直接到MYSQL命令行下试试你的语句,以断定是你程序的问题还是数据库的问题。
      

  4.   

    rrkaximodo (rrkaximodo)
      '截至2011-07-18 17:38:02  用户结帖率0.00% 
    当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html8、如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖
      

  5.   

    activity:author
    mem_friends:main_name client_name isok 上建立复合索引,
    将OR->UNION ALL
      

  6.   


    不好意思 看得不是很明白 要怎样union all?
      

  7.   

    select * from activity where author='$u_id' 
    union all
    select * from activity a  inner join mem_friends b 
    on a.author=b.client_name
    where main_name='$u_id' and client_name!='$u_id' and isok='yes'
      

  8.   


    好像不行哦提示:
    #1222 - The used SELECT statements have a different number of columns
    union all的两个select要列数一样吧?
      

  9.   


    ...
    union all -- 没必要重复的吧
    select a.*
    ...
      

  10.   

    shine333的代码是正确的union all 前后select字段要一致谢谢大家了