我正在努力解决具有挑战性的问题。
我有两张桌子:answers
,posts
ID
有PID
,name
,answers
ID
有PID
,approved
,ID
PID
是用户ID,posts.name
是帖子ID。
我要做的是列出所有帖子名称(ID
)以及有多少独特人物(answers.approved = 'Y'
s)回复了他们的帖子。诀窍是我只想计算被批准的回复(post.ID != answers.ID
)并且我不想计算创建帖子的人,所以(Name People with Approved Replies
Name 1 10
Name 2 7
)。
所以结果应该显示如下:
$results = $dbh->prepare("select COUNT(DISTINCT answers.ID) AS reply,
posts.ID,
posts.PID,
posts.name,
answers.PID,
answers.approved
FROM answers
LEFT JOIN posts ON answers.PID=posts.PID
WHERE (answers.approved = 'Y') LIMIT 10
GROUP BY answer.ID");
$results->execute();
$row = $results->fetchAll(PDO::FETCH_ASSOC);
我目前的代码是乱七八糟的方法。它只给出一个结果,回复的数量远远/过高。但这就是我目前所拥有的:
<StackPanel>
<local:MenuFlyoutService.MenuFlyout>
<MenuFlyout>
<!-- using the Click event -->
<MenuFlyoutItem Text="reply" Click="OnReplyClicked"/>
<!-- using commanding to DataContext of MenuItem -->
<MenuFlyoutItem Text="retweet" Command="{Binding RetweetCommand}"/>
<!-- using commanding to DataContext of parent list -->
<MenuFlyoutItem Text="favorite"
Command="{Binding DataContext.FavoriteCommand,
ElementName=TweetList}"
CommandParameter="{Binding}"/>
</MenuFlyout>
</local:MenuFlyoutService.MenuFlyout>
<!-- content for template goes here -->
</StackPanel>
答案 0 :(得分:1)
我认为这个查询应该做你想做的事情
SELECT
p.name,
p.id,
p.pid,
(SELECT COUNT(DISTINCT id)
FROM answers
WHERE answers.pid = p.pid
AND answers.approved = 'Y'
AND answers.id != p.id) as reply
FROM posts p
这将选择帖子名称,ID,用户ID和不同批准答案的数量