三个表中的SQL SELECT,在投票系统中排序

时间:2016-03-17 16:11:09

标签: php mysql sql laravel join

我的laravel项目中有一个投票系统我每个帖子都有很多答案,每个答案都有投票,我试着显示一个有投票号码的帖子的答案。这三个表:

  1. 帖子:id,title,content ...
  2. 回答:id,post_id,content ...
  3. answer_votes :id,post_id,answer_id,user_id,vote (如果没有人投票给答案,则为1或0或没有任何内容)
  4. answer_votes表中的

    user_id是为了防止用户多次投票。 我试着这样做:

    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 );
    

    但它不起作用...请帮我解决这个问题。

4 个答案:

答案 0 :(得分: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

你可以在这里看到 - > http://sqlfiddle.com/#!9/357aef/1

注意:这不包括尚无人投票的答案。

编辑:我知道您想要包含尚无人投票的答案。您可以通过稍微修改上述查询来执行此操作:

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)

观察到现在没有选票“Black”的投票。此查询将产生以下结果集:

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

你可以在这里看到 - > http://sqlfiddle.com/#!9/c4a9e5/2

希望这有帮助!!!

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您需要INNER JOIN来获取相关表格的数据

尝试这样的事情:

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;

答案 3 :(得分:0)

这里有很多答案在SQL中。我将以PHP回答。

所以这适用于Laravel。

您需要有3个型号。运行php artisan make:model制作模型(我猜你已经知道了)。

Post.phpAnswer.phpVotes.php

使用Eloquent建立关系。

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.

几乎就是这样。