Ok, I have an interesting problem here. I have two tables, tips and voices. The tips are as follows:
tip_id, challenge_id, user_id, ip_address, tip_date, tip_text
The voices are as follows:
tip_id, ip_address, vote_date, vote
Users can post call tips, and then other users can vote at the tip of their finger up or down. Voting field in votes - bool, 0 - thumb, 1 - thumb up. A task can have any number of tips.
I tormented my brain, and I just can't get it to work. I need a query that can get the number of votes up and down for a specific hint, the difference of these two numbers (so if there are 2 votes and 1 vote down, the difference will be 1) as a rating, and also tell me the data from the hint table, then first Sort it with the highest rating. Suppose we have two hints: a and b:
a: 3 up, 2 down (difference: 1) b: 4 up, 1 down (difference: 3)
In this case, b will be the first, since it has a higher rating than a, a greater difference between the number of votes up and down.
So, for example, let's say that I have a hint with id 1 that has 4 votes and 2 votes, which means a difference of 2, and this tip is related to calling id 5, my return might look like this
up | down | rating | tip_id | tip_date | user_id | tip_text
------------------------------------------------------------
3 | 2 | 1 | 1 | 12345678 | 1 | "this is a tip"
, , , , . , id 5 2 , :
up | down | rating | tip_id | tip_date | user_id | tip_text
------------------------------------------------------------
3 | 2 | 1 | 1 | 12345678 | 1 | "this is a tip"
4 | 1 | 3 | 1 | 87654321 | 2 | "this is also a tip"
. , , , . , , 7 3, .
, , , , PHP. , X , X - .
, , . , , .
: , . , , :
SELECT
COUNT(CASE WHEN V.vote = 1 THEN 'up' ELSE NULL END) AS up,
COUNT(CASE WHEN V.vote = 0 THEN 'down' ELSE NULL END) AS down,
(COUNT(CASE WHEN V.vote = 1 THEN 'up' ELSE NULL END)-COUNT(CASE WHEN V.vote = 0 THEN 'down' ELSE NULL END)) AS rating,
T.*
FROM votes V, tips T
WHERE T.challenge_id = 10
GROUP BY V.tip_id
ORDER BY rating DESC
, , , ( , ). , , , , . , , 3 1 , 2, , 6 2 4. , PHP, , . - , , ? , , LEFT JOIN, , , .