Count integer value and order by user - php

So my table looks like this:
| id | user | points |
| 1 | Sam | 1 |
| 2 | Sam | 6 |
| 3 | Phil | 1 |
The query I am currently using is:
SELECT user,COUNT(*) FROM table GROUP BY user order by COUNT(*) DESC
This returns the current value:
Sam: 2
Phil: 1
It looks like it counts the number of rows, not the total points? How can I do this?
The correct return should be Sam: 7.

Use SUM instead of COUNT
SELECT user, SUM(points) FROM table GROUP BY user

Related

SELECT, group, limit based on month

Currently I am using this: SELECT ident,COUNT(*) FROM sales GROUP BY ident order by COUNT(*) DESC LIMIT 3.
I use that to group each ident from sales table, and count the number of rows for each ident, and limit this to 3 rows. Now I want to only select the rows that were added the current month.
This is how the table looks like, sales table:
| ID | ident | prdnr | transfer |
| 1 | HD762 | 7362781 | 2020-08-10 16:25:26 |
| 2 | JJ313 | 4563456 | 2020-08-08 16:25:26 |
| 3 | HD762 | 4363453 | 2020-08-08 16:25:26 |
| 4 | JJ313 | 2326256 | 2020-08-08 16:25:26 |
| 5 | HD762 | 8356345 | 2020-08-07 16:25:26 |
| 6 | JJ844 | 3473563 | 2020-08-07 16:25:26 |
I think this should be the correct query:
SELECT ident,COUNT(*) FROM sales WHERE MONTH(transfer) = MONTH(CURRENT_DATE()) AND YEAR(transfer) = YEAR(CURRENT_DATE()) GROUP BY ident order by COUNT(*) DESC LIMIT 3
Somehow this does not gives me the correct count. What am I doing wrong?
Based on your requirement , a simple way is getting the MySQL current time and subtract a month to get the values
The code :-
SELECT ident,COUNT() FROM sales WHERE transfer > date_sub(now(),
interval 1 month) GROUP BY ident order by COUNT() DESC LIMIT 3;
The output from the query
HD762 3
JJ313 2
JJ844 1
Please let me know if this is the expected result you're looking for.
Reference :-
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html

MySQL - Average of Unique rows

If I have three columns:
id, user, points
My data is:
+-------+------------------+-------------+
| id | user | points |
+-------+------------------+-------------+
| 1 | A | 100 |
+-------+------------------+-------------+
| 1 | A | 200 |
+-------+------------------+-------------+
| 2 | B | 300 |
+-------+------------------+-------------+
| 2 | B | 400 |
+-------+------------------+-------------+
I would like to have the average of ONLY the max points of each user.
For this exmple I want to get as results: 300 points ((200+400)/2).
When I use the following Mysql query, I get: 250:
SELECT avg(points) FROM table
SQL DEMO
Try this :
SELECT avg(points) FROM (
SELECT max(points) as points FROM table1 group by id
) as T
Firstly get the max points of each user and then get the AVG from them.
You should first get the max group by use and the the avg of this subquery
SELECT AVG(points)
FROM (SELECT MAX(points) FROM your_table GROUP BY user) subt

Mysql select UNIQUE row on column

I have a table as following:(Ex)
id | vid | time
------------------------
1 | 4 | 1333635317
2 | 4 | 1333635323
3 | 2 | 1333635336
4 | 4 | 1333635343
5 | 5 | 1333635349
I want to be just a row (the last row [ID: 4]) of the same rows[id:1,2,4], how it will output the query?
I mean, as a result of these:
id | vid | time
------------------------
3 | 2 | 1333635336
4 | 4 | 1333635343
5 | 5 | 1333635349
What do i do?
i trying it as:
SELECT * from tbale as t1 where vid = 4 GROUP BY vid ORDER BY id DESC
but doesn't work ORDER BY in my query.
Get the max time per vid and use in to get those rows from the table.
select * from tablename
where (vid,time) in (select vid,max(time)
from tablename
group by vid)
order by id

Php/mysql ranking system - Placement inside a result set ordered by column

In a blog-like website, all the users can "star" a news (= bookmark it, mark it as "favourite").
I have a mysql table for stats.
table_news_stats
id_news
total_stars (int) //Total number of users who starred this news
placement (int)
The placement field is intuitive: if you order all the news by the total_stars field you get each news placement. So, the news with most stars will be number 1, and so on.
So, suppose I have 700 records in my table_news_stats, and for each one I have the id and the total_stars count, how can I update the placement field automatically for each record? Which query is faster/better?
Example of the table_news_stats content:
First record (A):
1-3654-?
Second record (B):
2-2456-?
Third record (C):
3-8654-?
If you order the record by stars count:
the sequence of records is C - A - B
So... the result will be:
First record (A):
1-3654-2
Second record (B):
2-2456-3
Third record (C):
3-8654-1
Clarification:
why would I ever need the placement field at all?
It's pretty simple... the placement field will be populated by a cronjob the first day of every month. Basically it will provide a 'snapshot' of the rank of each news in terms of popularity (as it was at the beginning of the current month). As a consequence, thanks to the placement field, I will have the following information:
"The 1st day of this month the 'top starred' news list was like this:
1- News C
2- NewsA
3- News B "
Then, with a query "SELECT * FROM table_news_stats ORDER BY total_stars DESC" I can obtain the new ranking (in real-time).
As a consequence, I will have the following information:
"At the time the page is loaded, the 'top starred' news list is like this:
1- News A
2- News C
3- News B "
Finally, by comparing the two rankings, I obtain the last piece of information:
"News A has gained a position" +1
"News C has lost a position" -1
"News B has no change in position" +0
If there is a better way of doing this, let me know.
I guess you don't need to update the table just:
SELECT *
FROM table_news_stats
ORDER BY total_stars DESC
But if you want to know the place of each one you can:
SELECT *, IF(#idx IS NULL,#idx:= 1,#idx:= #idx+1)
FROM table_news_stats
ORDER BY total_stars DESC
And if you still need to update something like:
UPDATE table_news_stats
SET placement = FIND_IN_SET(id_news,(SELECT GROUP_CONCAT(t.id_news) FROM (SELECT id_news
FROM table_news_stats
ORDER BY total_stars DESC) t ))
SQLFiddle
Consider the following
mysql> select * from test ;
+------+-------------+-----------+
| id | total_stars | placement |
+------+-------------+-----------+
| 1 | 3 | 0 |
| 2 | 6 | 0 |
| 3 | 7 | 0 |
| 4 | 2 | 0 |
| 5 | 9 | 0 |
| 6 | 2 | 0 |
| 7 | 1 | 0 |
+------+-------------+-----------+
Now using the following you can update the placement as
update test t1 join
(
select *,
#rn:= if(#prev = total_stars,#rn,#rn+1) as rank ,
#prev:= total_stars
from test,(select #rn:=0,#prev:=0)r
order by total_stars desc
)t2
on t2.id = t1.id
set t1.placement = t2.rank ;
mysql> select * from test order by placement ;
+------+-------------+-----------+
| id | total_stars | placement |
+------+-------------+-----------+
| 5 | 9 | 1 |
| 3 | 7 | 2 |
| 2 | 6 | 3 |
| 1 | 3 | 4 |
| 4 | 2 | 5 |
| 6 | 2 | 5 |
| 7 | 1 | 6 |
+------+-------------+-----------+
Note that in case of tie will have the same placement.

how to find the highest value in mysql table

i have a mysql table i.e
st_id | name | email | maths | chemistry | bio | social_study
1 | john |#a.com | 20 | 23 | 10 | 15
my question is how can i find the highest subject score, the second last and so on
Note that all the subject fields have int(11) values
Break your database into 3 tables like:
Students:
st_id | name | email
1 | john |#a.com
Courses:
cr_id | name
1 | maths
2 | chemistry
3 | bio
4 | social_studies
StudentCourses:
st_id | cr_id | score
1 | 1 | 20
1 | 2 | 23
1 | 3 | 10
1 | 4 | 15
Now you can do:
SELECT s.name, MAX(sc.score) FROM Students s INNER JOIN StudentCourses sc ON s.st_id = sc.st_id;
SELECT * FROM <table>
ORDER BY <field> DESC
LIMIT <needed number of rows>
Example:
SELECT * FROM <table>
ORDER BY maths+chemistry+bio+social_study DESC
LIMIT 3
Strictly PHP method: I assume you want to maintain association with field names. In that case, just use asort($row); on each row in your query result, assuming you fetched the row as an array. asort will sort the array from lowest value to highest (with additional flags to tweak the results if needed), while maintaining keys. A foreach loop will then allow you to work with each key/value pair in the sorted order.
st_id | name | email | maths | chemistry | bio | social_study
1 | john |#a.com | 20 | 23 | 10 | 15
The query can be for top marks
SELECT id,GREATEST(mark,mark1,mark2) AS `top` FROM `students`

Categories