作者aleck945 (总是不知所措)
看板Database
标题Re: [SQL ] MYSQL group by 的SQL语法问题
时间Tue Aug 26 01:18:26 2008
※ 引述《visualPHP (brain)》之铭言:
: 我有一张表要JOIN 另外一张表 2次
: 表一 profiles ( id , name , address )
: 表二 friend ( fid , id , id2 , type )
: profiles = 个人资料
: friend = 朋友关系
: profiles.id 对应 friend.id
: 也对应 friend.id2
: id 是 id2 的 type 关系
: friend 的type enum( '好友名单' , '黑名单' )
: 我的目的是想要 计算出 profiles的某的人的人气
: 当有一个人家他好友 人气就+1 一个人加他黑名单 人气就-1
: 但是问题出现了 连续 join 两次 friend as f1 , friend as f2
: 设定 group by f1.type 也不对 设定 group by f2.type也部队
: 也不能设定 group by f1.type , f2.type
: 这可能是我观念有点不清楚
: 拜托高手给一点提示
v大似乎把自己的问题复杂化了, (如果误会v大的问题, 请告知, 会自D)
如果只是想要知道人气, 只要管 friend table 的 id2 就好了.
试试以下: (IF 指令请依自己的SQL系统更改=> DECODE or CASE)
SELECT p.name,SUM(IF(type='好友名单',1,-1)) popular
FROM friend f LEFT JOIN profiles p ON f.id2 = p.id
GROUP BY p.name
ORDER BY popular DESC
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 122.116.190.50
1F:推 visualPHP:解法真屌耶~~~太好了 08/26 19:21