PHP&SQL select highest score - php

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

Related

Order by a columns count where another column has a static value

I have a table which stores user items, the two key columns which I would like to use in this query are user_id and item_id. The id field in the example is not needed but just added to show these aren't the only two columns in the table.
----------------------
id user_id item_id
----------------------
1 1 324
2 1 324
3 3 324
4 2 230
5 4 324
The query which I would like to construct should return the top 10 users who have the most items with a specific item id.
So for example if I wanted to run the query against the item ID 324 I should get the following result.
-------------------
user_id item_count
-------------------
1 2
3 1
4 1
2 0
try this
select user_id , count(*) as item_count from table
where item_id = 324 group by user_id order by item_count desc limit 10
limit 10 will show you the top 10 users and order by desc sort from high to low.
However, the above query will not give you the 0 count as per your question. If you really want the zero count you can try this: (assuming your table name is userlist)
SELECT distinct user_id,
(select
count(*) from `userlist`
where user_id=u.user_id and item_id=324
) as item_count FROM `userlist` u
order by item_count desc
I couldn't create the database in my local, but I think this will do the trick
SELECT user_id, COUNT(item_id) as item_count
FROM TABLE_NAME
WHERE item_id = 324
GROUP BY item_id
ORDER BY item_count;

MySQL PHP Order From Another Table

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;

Selecting a distinct value and the sum of times it shows up in a column

I have an existing table with millions of entries (growing) that consists of:
userid|name|etc...
1 frank ...
1 frank ...
2 joe ...
5 sam ...
1 franky ...
What I need to do is return a table of:
place|name|total
1 franky 3
2 sam 1
3 joe 1
Where total is the SUM(userid = the distinct userid).
Currently I'm doing a query to SELECT DISTINCT userid from table and then foreach returned value in php, I'm doing another query to return the name and sum(userid = userid).
As you can assume, this is very taxing and takes a long time now with all of the values. Is there any way to speed this up by doing 1 query?
i think you need
SELECT #a:=#a+1 AS `place`, name, COUNT(userid) AS `total`
FROM `your_table`, (SELECT #a:= 0) AS a
GROUP BY userid
SELECT userid, COUNT(*)
FROM some_table
GROUP BY userid

Mysql select max maximum sum values

I have a database which contains some picture data and a linking table. The tables are build-up like this:
---pictures---
picid Lat Lon
1 5 6
2 7 31
3 31 43
4 -3 35
---user2pictures---
picid userid vote
1 1 1
1 2 1
3 1 -1
3 2 1
4 2 -1
The table pictures contains a picture id and some data about the image, the table user2votes contains vote data from the images. Each user is able to vote on images, but they can only vote 1 time, so the vote will be either 1 (like) or -1 (dislike).
I want to select everything from the pictures table from pictures which have the highest number of votes. Pseudoquery which might explain better what I want:
SELECT * FROM pictures WHERE (SELECT MAX(SUM(vote)) FROM user2pictures LIMIT 12
In this example picture 1 would return at the top, picture 3 would follow and picture 4 as the last one. I really don't know how to solve this, some help in the right direction would be very much appreciated!
Thanks!
The answer is to JOIN the tables, SUM the votes, and ORDER high-to-low by the sum of the votes
SELECT pictures.*, SUM(user2pictures.vote) AS VoteTotal
FROM pictures
JOIN user2pictures ON pictures.picid = user2pictures.picid
GROUP BY pictures.picid
ORDER BY VoteTotal DESC
LIMIT 12
try this
select p.`picid`, `Lat`, `Lon` from pictures p
inner join user2pictures u2p
on p.picid = u2p.picid
group by u2p.picid
order by sum(vote) desc
limit 12
DEMO
I'll assume that you also want to show the pictures with no votes. So, you can try this:
select
p.picId, sum(v.vote) as votes
from
pictures as p
left join user2pictures as v on p.picId = v.picId
group by
p.picId
order by
sum(v.vote) desc
limit 12;
The left join is what lets you show the pictures with no votes (the column votes will have the value 0)
Hope this helps

Order by ID if two users have same number of credits

:-)
I have this script, which find a users position taken from the number of credits.
It all works, but i have a little problem. If two users have the same credits, both of them will be on the same position.
Can I do, so if there are more users with same credits, then the system need to order by the users ID and out from that give them a position?
This is my code so far:
$sql = "SELECT COUNT(*) + 1 AS `number`
FROM `users`
WHERE `penge` >
(SELECT `penge` FROM `users`
WHERE `facebook_id` = ".$facebook_uid.")";
$query_rang = $this->db->query($sql);
So if i have this:
ID -------- Credits
1 -------- 100
2 -------- 100
3 -------- 120
Then the rank list should be like this:
Number 1 is user with ID 3
Number 2 is user with ID 1
Number 3 is user with ID 2
ORDER BY credits DESC, id ASC. This will sort by credits and break ties with the id.
UPDATE
I understand now that you want the ranking information for the user, not just to sort the users by credits and ids. This will give you the complete list of users and their rankings:
SELECT #rank:=#rank+1 AS rank, users.id, users.facebook_id FROM users, (SELECT #rank:=0) dummy ORDER BY penge DESC, id ASC
Getting the row number is the tricky bit solved by this blog post:
http://jimmod.com/blog/2008/09/displaying-row-number-rownum-in-mysql/
$sql = "SELECT COUNT(*) + 1 AS `number` FROM `users` WHERE `penge` > (SELECT `penge` FROM `users` WHERE `facebook_id` = ".$facebook_uid.") ORDER BY COUNT(*) + 1 desc, users.ID";
$query_rang = $this->db->query($sql);
Later EDIT:
I don't understand why you still have the same results....
I made a quick test. I have created a table:
Test: ID (Integer) and No (Integer)
I have inserted some values:
id no
1 1
1 1
1 1
2 1
3 1
4 1
4 1
5 1
Now, if I run:
SELECT
id, COUNT(*) + 1 AS `number`
FROM
test
GROUP BY
id
I get:
id number
1 4
2 2
3 2
4 3
5 2
But if I add ORDER BY:
SELECT
id, COUNT(*) + 1 AS `number`
FROM
test
GROUP BY
id
ORDER BY
count(*) desc, id
then I get:
id number
1 4
4 3
2 2
3 2
5 2

Categories