我需要计算一个帖子的所有赞成票和赞成票,
并且只应显示用户是否是其他用户的朋友。
我的问题是,upvotes和downvotes的计数不准确。
示例:
post_id 5和4只有1个upvote,但检索到的数据表示他们有3个upvotes。
Here's我创建的sqlfiddle。
答案 0 :(得分:1)
你可以尝试使用这个小改动它返回预期的输出:
import numpy as np
from matplotlib import pyplot as plt
###############################################################################
# The data (change all of this to your actual data, this is just a mockup)
variables = [
'apple',
'juice',
'orange',
'peach',
'gum',
'stones',
'bags',
'lamps',
]
base = 3000
lows = np.array([
base - 246 / 2,
base - 1633 / 2,
base - 500 / 2,
base - 150 / 2,
base - 35 / 2,
base - 36 / 2,
base - 43 / 2,
base - 37 / 2,
])
values = np.array([
246,
1633,
500,
150,
35,
36,
43,
37,
])
###############################################################################
# The actual drawing part
# The y position for each variable
ys = range(len(values))[::-1] # top to bottom
# Plot the bars, one by one
for y, low, value in zip(ys, lows, values):
# The width of the 'low' and 'high' pieces
low_width = base - low
high_width = low + value - base
# Each bar is a "broken" horizontal bar chart
plt.broken_barh(
[(low, low_width), (base, high_width)],
(y - 0.4, 0.8),
facecolors=['white', 'white'], # Try different colors if you like
edgecolors=['black', 'black'],
linewidth=1,
)
# Display the value as text. It should be positioned in the center of
# the 'high' bar, except if there isn't any room there, then it should be
# next to bar instead.
x = base + high_width / 2
if x <= base + 50:
x = base + high_width + 50
plt.text(x, y, str(value), va='center', ha='center')
# Draw a vertical line down the middle
plt.axvline(base, color='black')
# Position the x-axis on the top, hide all the other spines (=axis lines)
axes = plt.gca() # (gca = get current axes)
axes.spines['left'].set_visible(False)
axes.spines['right'].set_visible(False)
axes.spines['bottom'].set_visible(False)
axes.xaxis.set_ticks_position('top')
# Make the y-axis display the variables
plt.yticks(ys, variables)
# Set the portion of the x- and y-axes to show
plt.xlim(base - 1000, base + 1000)
plt.ylim(-1, len(variables))
答案 1 :(得分:0)
看看这是否会让你到处(在SQLite中测试,网站无法正常工作)。
SELECT vote_type, post_id, user_id, COUNT(*) FROM (
SELECT * FROM post JOIN vote ON vote.post_id = post.post_id WHERE (post.user_id || "," || vote.user_id) IN (
SELECT DISTINCT user_id || "," || friend_id FROM friend WHERE request = 'friend'
)
) GROUP BY vote_type;
这个查询的一个大问题是连接(||),我相信它可以更加健壮。
答案 2 :(得分:0)
出现问题是因为user_id = 8发出了3个好友请求,因此您的查询为每个请求生成一行,您只需要一行 - 如果您更改了这样的查询,则可以避免多次计数:
SELECT user_info.id,user_info.fb_id,user_info.profile_pic,
user_info.fname,user_info.lname, post.post_id,post.message,post.time,post.img,post.user_id,
COUNT(CASE WHEN vote.vote_type = 'upvote' then 1 ELSE NULL END) AS 'upvote', COUNT(CASE WHEN vote.vote_type = 'downvote' then 1 ELSE NULL END) AS 'downvote'
FROM user_info
JOIN post
on post.user_id = user_info.id
LEFT JOIN vote ON
vote.post_id = post.post_id
WHERE EXISTS (select * FROM friend
WHERE (friend.user_id = user_info.id OR friend.friend_id = user_info.id)
AND friend.request = 'friend' AND (friend.user_id = 8 OR friend.friend_id = 8))
GROUP BY post.post_id
ORDER BY post.post_id DESC