我有三张桌子:users(id, account_balance)grocery(user_id, date, amount_paid)fishmarket(user_id, date, amount_paid)双管齐下fishmarket和grocery对于同一个USER_ID,表可能有多个出现,但支付的日期和金额不同,或者对任何给定用户都没有。当我尝试以下查询时:SELECT
t1."id" AS "User ID",
t1.account_balance AS "Account Balance",
count(t2.user_id) AS "# of grocery visits",
count(t3.user_id) AS "# of fishmarket visits"FROM users t1LEFT OUTER JOIN grocery t2 ON (t2.user_id=t1."id")
LEFT OUTER JOIN fishmarket t3 ON (t3.user_id=t1."id") GROUP BY t1.account_balance,t1.idORDER BY t1.id它产生了一个不正确的结果:"1", "12", "12".但当我试图LEFT JOIN只对一个表产生正确的结果。grocery或fishmarket访问,这是"1", "3", "4".我在这里做错什么了?我正在使用PostgreSQL9.1。
3 回答
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
添加回答
举报
0/150
提交
取消