select count(user_id) from users where user_id in (select user_friend_id from friend where user_id=241 and state=1) union select count(user_id) from users where user_id in (select user_id from friend where user_friend_id=241 and state=1)这个语句分别得到的是2和6,能不能把它合成一个句子得到的数是8?

解决方案 »

  1.   

    select sum(a) + sum(b)
    from(
    select count(user_id)a,0 as b from users where user_id in (select user_friend_id from friend where user_id=241 and state=1) union select 0,count(user_id) from users where user_id in (select user_id from friend where user_friend_id=241 and state=1))u
      

  2.   

    [code=SQ]--试试这样
    select count(*)
    from users
    where exists(select 1 from friend 
                 where ((user_id=241 and users.user_id=friend.user_friend_id)
                    or (user_friend_id=241 and users.user_id=friend.user_id))
                    and state=1)
    [/code]
      

  3.   

    自己构造,一是用子查询,两个查询作为表来把里面得到的数据相加;二是可以把得到的数据作为一个字段或者多个字段,在外边嵌套一个求和的查询;三。方法SOSO多。
      

  4.   

    楼上正解
    你还可以把两个where条件用or并到一起,这样出来的就是8条