I have two tables. One of them contains quotation marks, and the other contains all voice data (+1 or -1) for each quote. For demonstration purposes, I made simplified versions of two tables:
Quotes
+
Votes
+----+-------+------+ | ID | quote | vote | +----+-------+------+ | 1 | 1 | -1 | | 2 | 1 | -1 | | 3 | 3 | 1 | | 4 | 3 | -1 | | 5 | 3 | 1 | | 6 | 3 | -1 | | 7 | 4 | 1 | | 8 | 4 | 1 | | 9 | 4 | 1 | +----+-------+------+
I would like to list all the quotes on my website and show the corresponding vote count. First, the SQL query must read all the quotes and subsequently join the vote table. However, he must finally list the sum of all votes for each quote. The result of the SQL query will look like this:
+----+-----------------+------+ | ID | quote | vote | +----+-----------------+------+ | 1 | If you stare... | -2 | | 2 | Don't cry... | NULL | | 3 | Those that... | 0 | | 4 | Find a job... | 3 | +----+-----------------+------+
What does the SQL query look like described above?
Fabianius
source share