getting count of players from mysql - mysql

I need to get the amount of users who have rank = player
So far I tried select count(*) as count_players from users where rank = player
I'm not sure where the error is, if only in the tags and query is correct or I'm going about it completely wrong, thanks in advance for the advice!
table: [users]
id
username
password
rank
1
john
$2y$10$zYharAUmf36hVzkYUg87y.avY
player
2
jane
$2y$10$zYhajIUGU89887jhgUg87yKJ8G
admin
COUNT_PLAYERS = 1

Cully below is right, it shouldn't need to be grouped when you're after a single result. You would do the following when you were after it grouped by rank (and you wouldn't do rank='player').
SELECT COUNT(*) AS count_players FROM users WHERE rank='player';
OR SELECT COUNT(*) AS count_players FROM users GROUP BY rank if you wanted it grouped.
Have you tried quoting the rank you're targeting? It's a string, not a variable.

Related

How to combine GROUP BY and ORDER BY here?

I have a database with a table called 'messages'. The colums are 'sender', 'reseiver', 'subject', 'time' (the timestamp).
With my query, I want to get the entries grouped by the subject. BUT I want to have the corresponding time to be the latest.
So, for example, if this was my db:
person1 | person2 | hello | 2019-06-24 20:0**7**:00
person2 | person1 | hello | 2019-06-24 20:0**8**:00
I want to get this:
subject: hello
last entry: 2019-06-24 20:0**8**:00
I wasn't able to put the ORDER BY in front of the GROUP BY, so I tried a subquery:
SELECT subject, sender, receiver, time
FROM
(SELECT subject, sender, receiver, time
FROM messages
ORDER BY time DESC) AS subselect
GROUP BY subject
But it doesn't work and I can't figure out why.
Can somebody help me?
SELECT subject,sender,reseiver,max(time)
FROM messages
GROUP BY subject,sender,reseiver
ORDER BY max(time) DESC
You should group by sender,reseiver as well, otherwise those columns have unpredictable value. Or omit those column entirely.
I believe this is what you are looking for:
SELECT subject,sender,reseiver, MAX(time) AS time FROM messages GROUP BY subject
So, it works like this:
Gets relevant records (in this case -- all of them, as no WHERE provided)
Groups them by subject column, so now we have groups of records with the same subject
In every group it looks for max value of time column -- but this is per group, so, I guess that's what you wanted
And if you want to sort groups somehow, just add another ORDER BY at the end, without any subqueries.

MySQL: How to get TOP visited product for each user in a table?

I have a system with products. Everytime a user enters a product, I insert a record into my database.
I have a table with users and id_products, like this:
users id_product
____________________________
jondoe 2
george 9
jondoe 5
jondoe 2
george 9
george 9
george 2
I need a result (query) wich shows what is TOP visited product id for each user, so the result would be something like this:
jondoes most visited product is ID 2
georges most visitedproduct is ID 9
I was looking for the answer but I am not able to figure it out. Thanks a lot for your help, I appreciate it a lot.
Jan
This is a pain because it involves aggregation. One way to solve this uses a very complicated query. Another uses variables. A third method uses an aggregation trick that works under many circumstances:
select user,
substring_index(group_concat(id_product order by cnt desc), ',', 1) as mostCommonProduct
from (select user, id_product, count(*) as cnt
from t
group by user, id_product
) t
group by user;
One danger when using this method is that the intermediate result might be too long. You can set the group_concat_max_len system variable to get around that particular problem.

Selecting most recent as part of group by (or other solution ...)

I've got a table where the columns that matter look like this:
username
source
description
My goal is to get the 10 most recent records where a user/source combination is unique. From the following data:
1 katie facebook loved it!
2 katie facebook it could have been better.
3 tom twitter less then 140
4 katie twitter Wowzers!
The query should return records 2,3 and 4 (assume higher IDs are more recent - the actual table uses a timestamp column).
My current solution 'works' but requires 1 select to generate the 10 records, then 1 select to get the proper description per row (so 11 selects to generate 10 records) ... I have to imagine there's a better way to go. That solution is:
SELECT max(id) as MAX_ID, username, source, topic
FROM events
GROUP BY source, username
ORDER BY MAX_ID desc;
It returns the proper ids, but the wrong descriptions so I can then select the proper descriptions by the record ID.
Untested, but you should be able to handle this with a join:
SELECT
fullEvent.id,
fullEvent.username,
fullEvent.source,
fullEvent.topic
FROM
events fullEvent JOIN
(
SELECT max(id) as MAX_ID, username, source
FROM events
GROUP BY source, username
) maxEvent ON maxEvent.MAX_ID = fullEvent.id
ORDER BY fullEvent.id desc;

mySQL count occurances of a string

I have this query...
SELECT SUM(brownlow_votes) AS votes,
player_id,
player_name,
player_team,
COUNT(*) AS vote_count
FROM afl_brownlow_phantom, afl_playerstats
WHERE player_id=brownlow_player
AND brownlow_match=player_match
GROUP BY player_id
ORDER BY votes DESC LIMIT 50
So "votes" becomes the number of votes a player has, "vote_count" becomes the number of times (matches in which) a player has been voted for. This works fine.
However, I have another column called "brownlow_lock" which is either blank, or 'Y'. How do I get the number of occurances of 'Y'? I know I could solve this changing it to 0 or 1 and just doing a SUM() but I don't want to have to go and edit the tons of pages that are inserting data.
If I have understood you correctly you just need to add
COUNT(CASE WHEN brownlow_lock='Y' THEN 1 END) AS Cnt
to your query
Try using the IF control flow function
SELECT SUM(IF(brownlow_lock='Y',1,0)) lock_count ...

User ranking position in a table?

I have a game application, I'm keeping track of the number of games won per user, something like:
// table: users
id | username | num_games_won
How would I tell a user their overall ranking in terms of num_games_won? For example:
id | username | num_games_won
-----------------------------------
723 john 203
724 mary 1924
725 steve 391
The rankings would be: mary->0, steve->1, john->2. Given a username, how would I find their ranking number? (I'm using mysql)
Thanks
Try counting the number of users that have more games won that the user you are interested in (by id or username)
SELECT COUNT(*) FROM users
WHERE num_games_won > (SELECT num_games_won FROM users WHERE id = 723)
In this case you can do a simple ORDER BY. Unfortunately, MySQL doesn't support the nice analytical functions like RANK or ROWNUMBER that other databases support, because those would be other potential solutions when the answer isn't as simple as ORDER BY.
(edit: you can sort of cheat and simulate ROWNUMBER in MySQL thanks to this answer on SO)
In this case, you'd do SELECT * FROM users ORDER BY num_games_won DESC and the first row would have the most, the second would have second most, etc.