MySQL PHP Order From Another Table - php

I have a question.
I have 2 tables, one includes the comments and the other one includes the votes of these comments.
my comments table:
--------------------
comment_id | comment
1 abc1
2 abc2
3 abc3
4 abc4
--------------------
my voting table:
------------------
user_id comment_id | voted
1 1 1 // comment 1 has the result +1 now
2 1 1 // comment 1 has the result +2 now
3 1 2 // comment 1 has the result +1 now
4 4 1 // comment 4 has the result +1 now
5 4 2 // comment 4 has the result 0 now
------------------
Well, if a person likes a comment, it is saved as "1" to "voted". If a person dislikes a comment, it is saved as "2" to "voted".
$likes = $db->query('SELECT * FROM voting WHERE voted=1')->num_rows;
$dislikes = $db->query('SELECT * FROM voting WHERE voted=2')->num_rows;
$the_result = $likes-$dislikes;
For example, When 5 people liked a comment and 2 people disliked the same comment, the result I show is "+3".
I want to sort them using the greatest result.
Like: the first comment to show will have +4, the second one to show will have +2, the third one to show will have -2.
I want to do this in PHP. Thanks for helping.
Sorry for my bad explanation, this is my first question here. :(

I would try the following:
SELECT *,
(count(CASE WHEN vote = 1 then 1 ELSE NULL END) - (count(CASE WHEN vote = 2 then 1 ELSE NULL END))) as RESULT
FROM comments AS c
LEFT JOIN votes as v ON c.comment_id=v.comment_id
GROUP BY c.id
ORDER BY RESULT desc

Counting the votes:
Return only comment_id and the total votes:
Check the code
SELECT comment_id, SUM(
CASE voted
WHEN 1 THEN 1
ELSE -1
END) AS total
FROM voting
GROUP BY comment_id
ORDER BY total DESC;
Return with comments that has votes: Check the code
SELECT comment_id, comments, SUM(
CASE voted
WHEN 1 THEN 1
ELSE -1
END) AS total
FROM voting
INNER JOIN comments
USING(comment_id)
GROUP BY comment_id
ORDER BY total DESC;
Return all rows: Check the code
SELECT comment_id, comments, SUM(
CASE voted
WHEN 1 THEN 1
ELSE -1
END) AS total
FROM voting
RIGHT JOIN comments
USING(comment_id)
GROUP BY comment_id
ORDER BY total DESC;

Related

PHP&SQL select highest score

I'm going to select the highest result in my "quiz" table there is 5 type of data:
quizid, userid, quizdate, result, topicid
1 JAKE 1/7/2015 60 1
2 JAKE 1/7/2015 80 1
3 JAKE 1/7/2015 100 2
i wanna show each topic and user highest score only once.
But it only show the first highest score from the user, after user redo the quiz again it wont show the highest score.
Example: Userid JAKE has done first time quiz have 60 mark at quiz 1, when he redo quiz 1 and get 80 marks it still show 60mark at the Table.
Select userid,topicid, MAX(result) as result
FROM quiz GROUP BY userid, topicid
ORDER BY result desc
The final result should be show 80 for JAKE at topic 1, but my result was
1 JAKE 1/7/2015 60 1
3 JAKE 1/7/2015 100 2
Hey you should used it like. if you want complete record with max result
select * from (Select userid,topicid, result as result
FROM quiz ORDER BY result desc) as t GROUP BY t.userid, t.topicid
Try following,
Select userid, topicid, result from
(
Select
row_number() over (order by userid, topicid, result desc) row_id,
userid,
topicid,
result
FROM quiz
) t
where row_id = 1

" People Who Liked this Also Liked " Query in Mysql PHP

Music table
id | title
1 Rap God
2 Blank Space
3 Bad Blood
4 Speedom
5 Hit 'em up
Like table
u_id | m_id
1 1
1 2
1 4
1 5
2 3
2 4
2 5
3 1
3 5
4 1
4 2
4 5
Now if someone visits music with m_id = 1
Then the output might be like
m_id
5
2
4
To explain this a bit...
As m_id = 1 is liked by users -> {1,3,4} which in turn likes ->{2,4,5} musics. Since m_id=5 is liked by max number of users its first followed by m_id = 2 and m_id = 4.
My Try
I queried the users who liked m_id = 1
SELECT u_id FROM likes WHERE m_id =1
Then i stored in in an array and selected each of their likes and
arranged them in desc order of count.
But it is a very slow and long process is there any way i can do this ?
p.s I have heard of Association Rules and Bayesian theorem can be user to achieve this. But can anyone help me out with an example ?
You can JOIN back on the Like table and do something like this.
SELECT also_like.m_id, COUNT(also_like.m_id)
FROM [like] AS did_like
JOIN [like] AS also_like ON
also_like.u_id = did_like.u_id
AND also_like.m_id != did_like.m_id
WHERE did_like.m_id = 1
GROUP BY also_like.m_id
ORDER BY COUNT(also_like.m_id)
Essentially you are getting a list of users who liked an item then getting a complete list of those user's likes excluding the item they just liked.
You can then add a HAVING clause or LIMIT to filter things down a bit more.
using a subquery ...
SELECT m_id, count(u_id) as Rank FROM `like`
WHERE u_id in
(
SELECT u_id
FROM `like`
WHERE m_id = 1
)
AND m_id <> 1
GROUP BY m_id
ORDER BY Rank DESC
and optionally
LIMIT 0, 10
or how many "alsolikes" you want to display

Count likes of the latest date per user

I have a table "article_likes" of users and whether they like or dislike the article. With every like/dislike the datetime is saved and the id of the article. The like/dislike is saved as a bool in the "status" column.
id article_id user_id like_date status
1 8 2 2014-11-03 21:30:33 1
2 8 2 2014-11-03 21:31:00 0
3 8 3 2014-11-03 22:30:59 1
4 9 6 2014-11-03 22:36:25 1
5 9 2 2014-11-03 23:19:46 1
I like to get the amount of likes an article has. So only the status of the most recent date is valid for the count.
So the desired result would be:
article_id amount_likes
8 1
9 2
I want to get the result per article id, so not all at once.
Can anybody help me? Thanks!
This will solve your problem :)
SELECT article_id, SUM(IF(status = 1, 1, -1)) AS amount_likes FROM article_likes
GROUP BY article_id
If you store dislikes as -1 then:
SELECT article_id, SUM(status) AS amount_likes FROM article_likes
GROUP BY article_id
Try this:
SELECT `article_id`,COUNT(`user_id`) AS amount_likes FROM `links`
WHERE `status`='1' GROUP BY DATE(`like_date`), `user_id`;
Fiddle here - http://sqlfiddle.com/#!2/3b7bb6/4
Hope it helps
SELECT article_id, COUNT(IF(
SELECT CASE
WHEN status=1 THEN 'true'
ELSE 'false'
END),
'true','false'
)
)
FROM article_likes
GROUP BY article_id
ORDER BY article_id

MySQL Order by sum of another table

I currently have 2 tables in mysql; comments and comment_rating
comment_rating has the following structure:
+------------+
| Field |
+------------+
| id |
| comment_id |
| user_id |
| positive |
| created_at |
+------------+
The field positive is either 1 or -1 1 being a positive (up vote) and -1 being negative (down vote)
I have this current query which will get me the most rated on comment:
SELECT *, COUNT(comment_rating.id) AS rating_count FROM comments LEFT JOIN comment_rating ON comments.id = comment_rating.comment_id GROUP BY comments.id ORDER BY rating_count DESC
I Need to know how (using mysql query) I am able to get comments ordered by best rating;
Meaning ordered by the sum of from the rating per comment.
Example:
Comment X has 2 upvotes and 4 downvotes (grand total of -2)
Comment Y has no votes (grand total of 0)
Comment Z has 1 upvote (grand total of 1)
The order these will come out will be:
Comment Z
Comment Y
Comment X
SELECT comments.id,
COUNT(comment_rating.id) AS rating_count,
sum(positive) as rating
FROM comments
LEFT JOIN comment_rating ON comments.id = comment_rating.comment_id
GROUP BY comments.id
ORDER BY rating DESC
Thank you juergen d
Your query was almost perfect:
here is a slightly modified version which will do the ordering correctly:
SELECT comments.id,
COUNT(comment_rating.id) AS rating_count,
COALESCE(SUM(positive),0) as rating
FROM comments
LEFT JOIN comment_rating ON comments.id = comment_rating.comment_id
GROUP BY comments.id
ORDER BY rating DESC
This way if the row doesnt exist it will be set to zero rather than null and will be ordered correctly.

MySQL count and total

I have a table with a number of votes recorded in it, each vote goes against a post. What I want to be able to do is count the total occurances of each vote.
Table looks like this:
vote
-----
1
2
3
1
2
3
4
5
2
2
2
2
...and so on...
How can I count the records, e.g. In that list 1x2, 2x6, 3x2, 4x1 and 5x1 times.
select vote, count(votes) as vt
from t_your_table
group by vote
To get what you want you can use GROUP BY and COUNT:
SELECT vote, COUNT(*) AS cnt
FROM votes
GROUP BY vote
Result:
vote cnt
1 2
2 6
3 2
4 1
5 1
Votes that have zero count will not be represented in this result set. If you want to have these included with a count of zero then you will need to use an OUTER JOIN with a table listing all the possible votes.
SELECT
possible_votes.vote,
COUNT(votes.vote) AS cnt
FROM possible_votes
LEFT JOIN votes ON possible_votes.vote = votes.vote
GROUP BY possible_votes.vote
Result:
vote cnt
1 2
2 6
3 2
4 1
5 1
6 0
Check MySQL manual for GROUP BY combined with COUNT.
SELECT vote, COUNT(*) as total
FROM votes
GROUP BY vote

Categories