Php SQL从三个表中选择,并在投票系统中排序
在我的laravel项目中,我有一个投票系统,每个帖子都有很多答案,每个答案都有投票,我试着用投票数的顺序显示帖子的答案。这包括三个表格:Php SQL从三个表中选择,并在投票系统中排序,php,mysql,sql,laravel,join,Php,Mysql,Sql,Laravel,Join,在我的laravel项目中,我有一个投票系统,每个帖子都有很多答案,每个答案都有投票,我试着用投票数的顺序显示帖子的答案。这包括三个表格: 帖子:id、标题、内容 答案:id、帖子id、内容 答案投票:id、帖子id、答案id、用户id、投票(如果还没有人投票支持答案,则为1或0或无) 答案投票表中的用户id用于防止用户多次投票。 我试图做到这一点: select p.id,a.id,count(*) from posts p, answers a, answer_votes v where
select p.id,a.id,count(*) from posts p, answers a, answer_votes v
where a.id=v.answer_id and p.id=a.post_id and p.id=1
group by p.id,a.id;
select a.id from answers a, posts b
where a.post_id=1
and a.id in (select v.answer_id from answer_votes v where v.vote=1 group by v.answer_id )
or a.id not in (select v.answer_id from answer_votes v where v.answer_id=a.id and v.vote=1 group by v.answer_id );
但它不起作用。。。请帮我解决这个问题 您需要一个内部联接来获取相关表的数据
SELECT p.id
, p.title
, p.content
, a.id
, a.content
, SUM(ISNULL(av.vote,0)) AS TotalVotes
FROM posts AS p
INNER
JOIN answers AS a
on p.id=a.post_id
LEFT
OUTER
JOIN answer_votes AS av
on av.answer_id=a.id
GROUP
BY p.id
, p.title
, p.content
, a.id
, a.content
ORDER
BY TotalVotes
试着这样做:
select p.id,a.id,count(*) from posts p
INNER JOIN answers a ON p.id=a.post_id
INNER JOIN answer_votes v ON a.id=v.answer_id
where a.id=v.answer_id and p.id=a.post_id and p.id=1
group by p.id,a.id;
您需要一个内部联接来获取相关表的数据 试着这样做:
select p.id,a.id,count(*) from posts p
INNER JOIN answers a ON p.id=a.post_id
INNER JOIN answer_votes v ON a.id=v.answer_id
where a.id=v.answer_id and p.id=a.post_id and p.id=1
group by p.id,a.id;
由于您没有提供任何示例数据,我创建了一些自己的示例数据:
CREATE TABLE posts (id INT, title NVARCHAR(30), content NVARCHAR(200));
CREATE TABLE answers (id INT, post_id INT, content NVARCHAR(200));
CREATE TABLE answer_votes (id INT, post_id INT, answer_id INT, user_id INT, vote bit);
样本数据:
INSERT INTO posts VALUES(1, 'Favorite Color', 'Which is your favorite color?');
INSERT INTO posts VALUES(2, 'Favorite Cheese', 'Which is your favorite cheese?');
INSERT INTO answers VALUES(1, 1, 'Black');
INSERT INTO answers VALUES(2, 1, 'Red');
INSERT INTO answers VALUES(3, 1, 'Yellow');
INSERT INTO answers VALUES(4, 1, 'Green');
INSERT INTO answers VALUES(5, 1, 'Orange');
INSERT INTO answers VALUES(6, 2, 'Parmesan');
INSERT INTO answers VALUES(7, 2, 'Mozarella');
INSERT INTO answers VALUES(8, 2, 'Swiss Cheese');
INSERT INTO answers VALUES(8, 2, 'Colby-Jac');
INSERT INTO answers VALUES(9, 2, 'Monterey Jack');
INSERT INTO answer_votes VALUES(1, 1, 1, 100, 1);
INSERT INTO answer_votes VALUES(2, 1, 2, 101, 1);
INSERT INTO answer_votes VALUES(3, 1, 3, 102, 1);
INSERT INTO answer_votes VALUES(4, 1, 4, 103, 1);
INSERT INTO answer_votes VALUES(5, 1, 5, 104, 1);
INSERT INTO answer_votes VALUES(6, 1, 5, 105, 1);
INSERT INTO answer_votes VALUES(7, 1, 5, 106, 1);
INSERT INTO answer_votes VALUES(8, 1, 3, 107, 1);
INSERT INTO answer_votes VALUES(9, 1, 3, 108, 1);
INSERT INTO answer_votes VALUES(10, 1, 2, 109, 1);
INSERT INTO answer_votes VALUES(11, 1, 2, 110, 1);
INSERT INTO answer_votes VALUES(12, 1, 2, 111, 1);
INSERT INTO answer_votes VALUES(13, 1, 5, 112, 1);
INSERT INTO answer_votes VALUES(14, 1, 5, 113, 1);
INSERT INTO answer_votes VALUES(15, 1, 5, 114, 1);
INSERT INTO answer_votes VALUES(16, 1, 1, 115, 1);
INSERT INTO answer_votes VALUES(17, 2, 6, 100, 1);
INSERT INTO answer_votes VALUES(18, 2, 7, 101, 1);
INSERT INTO answer_votes VALUES(19, 2, 7, 102, 1);
INSERT INTO answer_votes VALUES(20, 2, 7, 103, 1);
INSERT INTO answer_votes VALUES(21, 2, 7, 104, 1);
INSERT INTO answer_votes VALUES(22, 2, 6, 105, 1);
INSERT INTO answer_votes VALUES(23, 2, 6, 106, 1);
INSERT INTO answer_votes VALUES(24, 2, 8, 107, 1);
INSERT INTO answer_votes VALUES(25, 2, 8, 108, 1);
INSERT INTO answer_votes VALUES(26, 2, 9, 109, 1);
INSERT INTO answer_votes VALUES(27, 2, 9, 110, 1);
INSERT INTO answer_votes VALUES(28, 2, 7, 111, 1);
INSERT INTO answer_votes VALUES(29, 2, 7, 112, 1);
INSERT INTO answer_votes VALUES(30, 2, 6, 113, 1);
INSERT INTO answer_votes VALUES(31, 2, 7, 114, 1);
INSERT INTO answer_votes VALUES(32, 2, 8, 115, 1);
您可以使用以下查询:
SELECT posts.id,
posts.title,
posts.content as Question,
ans.content as Answer,
COUNT(votes.answer_id) as TotalVotes
FROM answers ans
INNER JOIN posts posts on ans.post_id = posts.id
INNER JOIN answer_votes votes on ans.id = votes.answer_id
WHERE votes.vote = 1
GROUP BY votes.answer_id,
posts.id,
posts.title,
posts.content,
ans.content
ORDER BY posts.id,
COUNT(votes.answer_id) DESC
您将看到以下结果集:
id title Question Answer TotalVotes
1 Favorite Color Which is your favorite color? Orange 6
1 Favorite Color Which is your favorite color? Red 4
1 Favorite Color Which is your favorite color? Yellow 3
1 Favorite Color Which is your favorite color? Black 2
1 Favorite Color Which is your favorite color? Green 1
2 Favorite Cheese Which is your favorite cheese? Mozarella 7
2 Favorite Cheese Which is your favorite cheese? Parmesan 4
2 Favorite Cheese Which is your favorite cheese? Colby-Jac 3
2 Favorite Cheese Which is your favorite cheese? Swiss Cheese 3
2 Favorite Cheese Which is your favorite cheese? Monterey Jack 2
你可以在这里看到->
注:这不包括尚未投票的答案
编辑:我知道您希望包含尚未投票的答案。您可以通过如下方式稍微修改上述查询来实现这一点:
SELECT posts.id,
posts.title,
posts.content as Question,
ans.content as Answer,
COUNT(votes.answer_id) as TotalVotes
FROM answers ans
LEFT JOIN answer_votes votes ON ans.id = votes.answer_id
LEFT JOIN posts posts ON ans.post_id = posts.id
GROUP BY posts.id,
posts.title,
posts.content,
ans.content,
votes.answer_id
ORDER BY posts.id,
COUNT(votes.answer_id) DESC
稍微修改上述数据集,如下所示:
INSERT INTO answer_votes VALUES(1, 1, 5, 100, 1)
INSERT INTO answer_votes VALUES(16, 1, 5, 115, 1)
请注意,现在没有人投票支持“黑色”的答案。此查询将生成以下结果集:
id title Question Answer TotalVotes
1 Favorite Color Which is your favorite color? Orange 8
1 Favorite Color Which is your favorite color? Red 4
1 Favorite Color Which is your favorite color? Yellow 3
1 Favorite Color Which is your favorite color? Green 1
1 Favorite Color Which is your favorite color? Black 0 /* Shows 0 votes for Black */
2 Favorite Cheese Which is your favorite cheese? Mozarella 7
2 Favorite Cheese Which is your favorite cheese? Parmesan 4
2 Favorite Cheese Which is your favorite cheese? Swiss Cheese 3
2 Favorite Cheese Which is your favorite cheese? Colby-Jac 3
2 Favorite Cheese Which is your favorite cheese? Monterey Jack 2
你可以在这里看到->
希望这有帮助 由于您没有提供任何示例数据,我创建了一些自己的示例数据:
CREATE TABLE posts (id INT, title NVARCHAR(30), content NVARCHAR(200));
CREATE TABLE answers (id INT, post_id INT, content NVARCHAR(200));
CREATE TABLE answer_votes (id INT, post_id INT, answer_id INT, user_id INT, vote bit);
样本数据:
INSERT INTO posts VALUES(1, 'Favorite Color', 'Which is your favorite color?');
INSERT INTO posts VALUES(2, 'Favorite Cheese', 'Which is your favorite cheese?');
INSERT INTO answers VALUES(1, 1, 'Black');
INSERT INTO answers VALUES(2, 1, 'Red');
INSERT INTO answers VALUES(3, 1, 'Yellow');
INSERT INTO answers VALUES(4, 1, 'Green');
INSERT INTO answers VALUES(5, 1, 'Orange');
INSERT INTO answers VALUES(6, 2, 'Parmesan');
INSERT INTO answers VALUES(7, 2, 'Mozarella');
INSERT INTO answers VALUES(8, 2, 'Swiss Cheese');
INSERT INTO answers VALUES(8, 2, 'Colby-Jac');
INSERT INTO answers VALUES(9, 2, 'Monterey Jack');
INSERT INTO answer_votes VALUES(1, 1, 1, 100, 1);
INSERT INTO answer_votes VALUES(2, 1, 2, 101, 1);
INSERT INTO answer_votes VALUES(3, 1, 3, 102, 1);
INSERT INTO answer_votes VALUES(4, 1, 4, 103, 1);
INSERT INTO answer_votes VALUES(5, 1, 5, 104, 1);
INSERT INTO answer_votes VALUES(6, 1, 5, 105, 1);
INSERT INTO answer_votes VALUES(7, 1, 5, 106, 1);
INSERT INTO answer_votes VALUES(8, 1, 3, 107, 1);
INSERT INTO answer_votes VALUES(9, 1, 3, 108, 1);
INSERT INTO answer_votes VALUES(10, 1, 2, 109, 1);
INSERT INTO answer_votes VALUES(11, 1, 2, 110, 1);
INSERT INTO answer_votes VALUES(12, 1, 2, 111, 1);
INSERT INTO answer_votes VALUES(13, 1, 5, 112, 1);
INSERT INTO answer_votes VALUES(14, 1, 5, 113, 1);
INSERT INTO answer_votes VALUES(15, 1, 5, 114, 1);
INSERT INTO answer_votes VALUES(16, 1, 1, 115, 1);
INSERT INTO answer_votes VALUES(17, 2, 6, 100, 1);
INSERT INTO answer_votes VALUES(18, 2, 7, 101, 1);
INSERT INTO answer_votes VALUES(19, 2, 7, 102, 1);
INSERT INTO answer_votes VALUES(20, 2, 7, 103, 1);
INSERT INTO answer_votes VALUES(21, 2, 7, 104, 1);
INSERT INTO answer_votes VALUES(22, 2, 6, 105, 1);
INSERT INTO answer_votes VALUES(23, 2, 6, 106, 1);
INSERT INTO answer_votes VALUES(24, 2, 8, 107, 1);
INSERT INTO answer_votes VALUES(25, 2, 8, 108, 1);
INSERT INTO answer_votes VALUES(26, 2, 9, 109, 1);
INSERT INTO answer_votes VALUES(27, 2, 9, 110, 1);
INSERT INTO answer_votes VALUES(28, 2, 7, 111, 1);
INSERT INTO answer_votes VALUES(29, 2, 7, 112, 1);
INSERT INTO answer_votes VALUES(30, 2, 6, 113, 1);
INSERT INTO answer_votes VALUES(31, 2, 7, 114, 1);
INSERT INTO answer_votes VALUES(32, 2, 8, 115, 1);
您可以使用以下查询:
SELECT posts.id,
posts.title,
posts.content as Question,
ans.content as Answer,
COUNT(votes.answer_id) as TotalVotes
FROM answers ans
INNER JOIN posts posts on ans.post_id = posts.id
INNER JOIN answer_votes votes on ans.id = votes.answer_id
WHERE votes.vote = 1
GROUP BY votes.answer_id,
posts.id,
posts.title,
posts.content,
ans.content
ORDER BY posts.id,
COUNT(votes.answer_id) DESC
您将看到以下结果集:
id title Question Answer TotalVotes
1 Favorite Color Which is your favorite color? Orange 6
1 Favorite Color Which is your favorite color? Red 4
1 Favorite Color Which is your favorite color? Yellow 3
1 Favorite Color Which is your favorite color? Black 2
1 Favorite Color Which is your favorite color? Green 1
2 Favorite Cheese Which is your favorite cheese? Mozarella 7
2 Favorite Cheese Which is your favorite cheese? Parmesan 4
2 Favorite Cheese Which is your favorite cheese? Colby-Jac 3
2 Favorite Cheese Which is your favorite cheese? Swiss Cheese 3
2 Favorite Cheese Which is your favorite cheese? Monterey Jack 2
你可以在这里看到->
注:这不包括尚未投票的答案
编辑:我知道您希望包含尚未投票的答案。您可以通过如下方式稍微修改上述查询来实现这一点:
SELECT posts.id,
posts.title,
posts.content as Question,
ans.content as Answer,
COUNT(votes.answer_id) as TotalVotes
FROM answers ans
LEFT JOIN answer_votes votes ON ans.id = votes.answer_id
LEFT JOIN posts posts ON ans.post_id = posts.id
GROUP BY posts.id,
posts.title,
posts.content,
ans.content,
votes.answer_id
ORDER BY posts.id,
COUNT(votes.answer_id) DESC
稍微修改上述数据集,如下所示:
INSERT INTO answer_votes VALUES(1, 1, 5, 100, 1)
INSERT INTO answer_votes VALUES(16, 1, 5, 115, 1)
请注意,现在没有人投票支持“黑色”的答案。此查询将生成以下结果集:
id title Question Answer TotalVotes
1 Favorite Color Which is your favorite color? Orange 8
1 Favorite Color Which is your favorite color? Red 4
1 Favorite Color Which is your favorite color? Yellow 3
1 Favorite Color Which is your favorite color? Green 1
1 Favorite Color Which is your favorite color? Black 0 /* Shows 0 votes for Black */
2 Favorite Cheese Which is your favorite cheese? Mozarella 7
2 Favorite Cheese Which is your favorite cheese? Parmesan 4
2 Favorite Cheese Which is your favorite cheese? Swiss Cheese 3
2 Favorite Cheese Which is your favorite cheese? Colby-Jac 3
2 Favorite Cheese Which is your favorite cheese? Monterey Jack 2
你可以在这里看到->
希望这有帮助 这里有很多答案都在SQL中。我将用PHP回答 这对拉威尔来说很有效 你需要有3个模型。运行
php artisan make:model
创建一个模型(我猜您已经知道了)
Post.php
,Answer.php
,vows.php
用雄辩来建立关系
在Post.php
文件上,建立一个one-to-many
关系,如下所示:
public function answers()
{
return $this->hasMany('App\Answer', 'post_id', 'id');
}
在Answer.php
文件上,创建另一个one-to-many
关系,如下所示
public function votes()
{
return $this->hasMany('App\Votes', 'answer_id', 'id');
}
现在,在同一文件上建立一个“属于”关系:
public function post()
{
return $this->belongsTo('App\Post', 'post_id', 'id');
}
现在,您应该能够执行以下操作:
$answer = Answer::first();
$answer->post //This should get you the post the answer belongs to.
$answer->votes //Retuns all the votes an answer has. You can iterate through this array.
差不多就是这样。这里有很多答案都在SQL中。我将用PHP回答 这对拉威尔来说很有效 你需要有3个模型。运行
php artisan make:model
创建一个模型(我猜您已经知道了)
Post.php
,Answer.php
,vows.php
用雄辩来建立关系
在Post.php
文件上,建立一个one-to-many
关系,如下所示:
public function answers()
{
return $this->hasMany('App\Answer', 'post_id', 'id');
}
在Answer.php
文件上,创建另一个one-to-many
关系,如下所示
public function votes()
{
return $this->hasMany('App\Votes', 'answer_id', 'id');
}
现在,在同一文件上建立一个“属于”关系:
public function post()
{
return $this->belongsTo('App\Post', 'post_id', 'id');
}
现在,您应该能够执行以下操作:
$answer = Answer::first();
$answer->post //This should get you the post the answer belongs to.
$answer->votes //Retuns all the votes an answer has. You can iterate through this array.
差不多就是这样。你想要它的PHP代码还是什么?@TaylorSwift是sql查询还是PHP代码。你想要它的PHP代码还是什么?@TaylorSwift是sql查询还是PHP代码。是的,我就是这么做的,但这个查询也不会得到尚未投票的答案,因为有三种投票的可能性:1票或0票,或者如果还没有人投票支持答案,就什么都没有。因此,此查询不获取第三种可能性,因为答案尚未添加到答案投票表中。是的,这就是我所做的,但此查询也不获取尚未投票的答案,因为有三种投票可能性:1或0,或者如果没有人投票支持答案,则不获取任何结果。因此,此查询不获取第三种可能性,因为答案尚未添加到答案投票表中。此检查的问题与此相同,您将了解问题所在我删除了答案id 1(黑色)的所有投票,但结果中未显示。您收到了吗?如果你不明白,告诉我。@HANZOSALAH我已经更新了我的答案!!请参见编辑的部分。这将为您提供没有投票的答案的结果。与此检查的问题相同,您将了解问题所在我删除了答案id 1(黑色)的所有投票,但它不会显示在结果中。您收到了吗?如果你不明白,告诉我。@HANZOSALAH我已经更新了我的答案!!请参见编辑的部分。这将为没有投票的答案提供结果。