
刚刚的代码忘记设为 null 了,现在改了一下可以符合预期
with student_transactions as (
select
transactions.student_id,
count(
transactions.id) as transactions_count
from transactions
group by transactions.student_id
)
select
students.id,
students.name,
coalesce(student_transactions.transactions_count, 0) as transactions_count
from students
left join student_transactions on
students.id = student_transactions.student_id