场景如下:微博表:id  |      content   |    userid用户关系表  id    |   userid     |     followid 现在想获取用户和自己的所有的微博信息, select content from weibo w where w.userid = (select followid from user_relation where userid = "1")  这样查询到的是好友的微博列表  怎么一次查询得到所有的信息? 子查询里面怎么加上自己的id呢?

解决方案 »

  1.   


       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  2.   


    你这条sql  有问题  应该是 in 吧现在想获取用户和自己的所有的微博信息?  这会用到子查询吗,一个很简单的Sql 就可以了,能不用子查询就不用。
      

  3.   

    select content from weibo w where w.userid = (select followid from user_relation where userid = "1")
    union all
    select content from weibo w where w.userid ="1"
      

  4.   


    恩 确实是IN 打错了 最后还是用 union解决的,我的sql这样
    select content from weibo w where w.userid in (select followid from user_relation where userid = "1" union select 1);