SQL: Sort List by Vote Numbers from Another Table

Asked

Viewed 97 times

0

I want the results of my poll to be sorted by the amount of votes you have in the other exclusive table to compute votes.

Currently my results come out like this: resultado atual

I have two tables, this one below belongs to the answers to be voted. The column enquete_id indicates the number of the poll that is linked to the voting responses.

CREATE TABLE `respostas` (
  `id` int(11) NOT NULL,
  `enquete_id` int(11) NOT NULL,
  `opcao` varchar(500) NOT NULL DEFAULT ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

And this table belongs to the votes, this is where they will be computed:

CREATE TABLE `votos` (
  `id` int(11) NOT NULL,
  `enquete_id` int(11) NOT NULL,
  `opcao_id` int(11) NOT NULL,
  `data` int(11) NOT NULL,
  `ip` varchar(100) NOT NULL DEFAULT ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

I tried something like that, but it didn’t work out, even the times it did, Unfortunately there were no zeroed votes, no options to vote that were without votes.

SELECT * 
FROM resposta op 
INNER JOIN votos vt ON (vt.enquete_id = op.enquete_id) 
WHERE op.enquete_id='1' 
GROUP BY op.id ORDER BY vt.opcao_id

Reference

  • 1

    This answers your question? Doubt LEFT JOIN SQL

  • I think that’s about it, but I still can’t do it.

1 answer

2


"Unfortunately there were no zeroed votes, no options to vote that were without votes". By that description and by your example, the problem is in join.

When using INNER JOIN, causes, a related record exists the same values in the two tables, i.e., the "enquete_id" must exist in both tables.

To bring the values "zero" IE, without votes, you need to make a LEFT Join (if you have questions, see this question: what is the difference between Ner Join and Uter Join), but this way, if the record does not exist in the "votes" table, it will return NULL, then you can use the Function ifnull to return zero instead of null, so the query looks like this:

SELECT COUNT(IFNULL(vt.enquete_id, 0)) votos
  FROM resposta op 
  LEFT JOIN votos vt ON (vt.enquete_id = op.enquete_id) 
 WHERE op.enquete_id='1' 
 GROUP BY op.id 
 ORDER BY vt.opcao_id, votos

EDIT: for example, with the specific data here: http://sqlfiddle.com/#! 9/309804/11 it was possible to notice that Join also needs to be done with the option voted, in which case the query was like this:

SELECT op.*, COUNT(vt.opcao_id) votos
  FROM resposta op 
  LEFT JOIN votos vt ON vt.enquete_id = op.enquete_id
        AND vt.opcao_id = op.id
 WHERE op.enquete_id = 1 
 GROUP BY vt.enquete_id, vt.opcao_id
 ORDER BY votos DESC, op.opcao;
  • 1

    Failed to change the order for the amount of votes. think q tb the SUM should be changed by a Count in the votes table, no?

  • yes well observed, I put the query that was to give the general idea, but I will change there is right, thank you :)

  • It almost worked, but hasn’t yet ordered by the highest number of votes.

  • just change the order by to how you need it, you can just votos desc for example

  • See the screenshot of how it is showing up: https://bit.ly/3nzLJsz Note that ID 44 has 1 vote and ID 43 has no votes, ID 44 should be above 43. Note: I put one op.*, after the SELECT to appear the other elements of the table, was appearing only "votes".

  • this shows nothing, go here: http://sqlfiddle.com/ create the tables and make Inserts with the values you have, then it is easier to help

  • Here is the link you requested: http://sqlfiddle.com/#! 9/309804/1/0 I am very grateful if you can help me.

  • 1

    seeing the data became easier, missing to do the Join tbm by option, see my answer

  • it worked out! Thank you!

  • do not forget to accept the answer :)

  • I detected a new problem: when the poll is new and has no votes on any options, the options to vote do not appear. If you add more options to vote for this link test, you will see that they will not appear. I made an edit for you to see: http://sqlfiddle.com/#! 9/fc4d23/2

  • if you use * select will not group and tbm needs to include all the fields you will use in select in the group by to ensure that it counts correctly. There are fields that do not repeat, such as date and ip, and it makes no sense to be in the query if you want to count occurrences. Take this example of how to solve: http://sqlfiddle.com/#! 9/fc4d23/23 The question already accepted, if you removed ask another question with another problem, it does not make sense to do this and soon it becomes a chat without end ok?

  • Oops! Even with more than 8 years of experience dealing with PHP and SQL, I would never be able to do such a query. Thank you, everything went well and I had a great learning!

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.