我有以下两张表
Table: items
ID | TITLE
249 | One
250 | Two
251 | Three
我对这些投票:
Table: votes
VID | IID | userid | votes
01 | 249 | 6 | 5
02 | 249 | 7 | -5
03 | 249 | 8 | 5
04 | 249 | 9 | 5
05 | 250 | 6 | -5
06 | 250 | 7 | -5
07 | 250 | 8 | 5
-5 表示投反对票,+5 表示投赞成票。假设我以用户 6 身份登录,SQL 查询会给我什么:
Table: result
ID | TITLE | TOTALVOTES | UPVOTES | DOWNVOTES | CURRENTUSERVOTED
249 | One | 4 | 3 | 1 | 1
250 | Two | 3 | 1 | 2 | 1
251 | Three | 0 | 0 | 0 | 0