我有一個表跟蹤用戶的可信度。計數返回NULL時,它應該只是零
+-----------------+-----------------+-------------+
| user_id_1 | user_id_2 | is_trusted |
+-----------------+-----------------+-------------+
| 1 | 2 | 0 |
...
其中1是可信的,0不是。如果沒有負面反饋,我會得到NULL的值。有沒有辦法獲得積極的 - 0?
select tr2.user_id_2 user,
((select count(*) plus
from trust_ratings tr
where is_trusted = 1 and
tr2.user_id_2 = tr.user_id_2
group by tr.user_id_2)
-
(select count(*) minus
from trust_ratings tr
where is_trusted = 0 and
tr2.user_id_2 = tr.user_id_2
group by tr.user_id_2)) as score
from trust_ratings tr2
group by user;
你逐一嘗試查詢? –
是的,他們給出了正確的數字。 –