Display value and number of duplicates in MySQL - mysql

I have a MySQL database row named ID and it goes
ID
18464762
3936573
3936573
3936573
374749502
374749502
374749502
374749502
374749502
3746325
9705732
9705732
9705732
9705732
476870382
476870382
3746574
37264
37264
And I want to make a MySQL Query that displays the information in two columns, the ID, and how many occurrences of the ID exist. That way I can sort it by number of occurrences.
The ideal output would be
id occurences
374749502 5
9705732 4
3936573 3
32764 2
476870382 2
18464762 1
3746325 1
3746574 1
This is just a small example as I have thousands of entries.
Everything I have already found from searching online tells me how to find which ids have duplicates, or the number of id's that have duplicates, but I have been unable to display the information like this.
Thank you in advance

You need to GROUP BY id, use aggregate function COUNT for counting occurences, and at the end order by second column.
SELECT id, COUNT(*) AS occurences
FROM your_tab
GROUP BY id
ORDER BY 2 DESC

Related

Selecting multiple same value rows out of the same column

I have a table, where one of the columns is named mid. It has a lot of values, some of them repeat themselves. Theres also a column named chashrate. It has a different value for each mid row. Theres also a column named pid, which shows the id of each row.
I've tried pulling out specific value rows with HAVING, but I can only do one value at a time or multiple values that dont match each other
$miner = $pdo->query("SELECT * FROM data WHERE pid='6'")->fetchall();
What I need to do is collect all the same MID column value rows, with the id pid=6 so for example all of the mid = 8; pid=6, collect their chashrate and sum it up. So for example I would get mid(8)=17394, mid(6)=28424 etc.
Here's a photo of the table: https://i.imgur.com/9xX6sYm.png
The same colored rows need to be selected and their chashrate values summed up.
Try using SUM to sum the cashrate values and GROUP BY to group them by mid.
SELECT mid
, SUM(`cashrate`) AS total
FROM `data`
WHERE pid = 6
GROUP BY mid;
Check it here.
For the given data on the image, this query will output the following result:
mid | total
6 | 981
8 | 374
You seem to want aggregation:
select mid, sum(chashrate) as sum_chashrate
from data
where pid = 6
group by pid, mid;
This will return multiple rows, one for each mid value.
You can do this for multiple pids -- or even all of them, by removing or changing the where clause.

Count the number of unique occurences in a MySQL column

I have a table with a column status:
1 row with status_1
3 rows with status_2
2 rows with status_3
7 rows with status_4
I want to write a query that gets all available statuses, groups them, and get the number of occurences then fetch the data as follows:
Status 1: 1
Status 2: 3
Status 3: 2
Status 4: 7
NB: The statuses are not already known. And we are not talking about DISTINCT or GROUP BY to count the number of unique values (it must be a separate number for each status)
SELECT status, count(*) count
FROM your_table
GROUP BY status;
See MySQL Group BY tutorial
According to data you seem want GROUP BY with COUNT() only
select status, count(*) as noofoccurences
from table t
group by status;

select records in given ids sorting order

i have table lets say - Students,
with 5 records and id(s) are 1 to 5, now i want to select the records - in a way that result should come like given sorting order of id column
id column should be resulted - 5,2,1,3,4
is there any other way to do this - then separate db calls for ids?
single db call ?
I guess if you really want a hard-coded order, you could do something like this:
order by case id
when 5 then 0
when 2 then 1
when 1 then 2
when 3 then 3
when 4 then 4
else 999
end
Or more simply (as #Strawberry points out in the comments):
order BY FIELD(id,4,3,1,2,5) desc

ORDER BY and GROUP BY those results in a single query

I am trying to query a dataset from a single table, which contains quiz answers/entries from multiple users. I want to pull out the highest scoring entry from each individual user.
My data looks like the following:
ID TP_ID quiz_id name num_questions correct incorrect percent created_at
1 10154312970149546 1 Joe 3 2 1 67 2015-09-20 22:47:10
2 10154312970149546 1 Joe 3 3 0 100 2015-09-21 20:15:20
3 125564674465289 1 Test User 3 1 2 33 2015-09-23 08:07:18
4 10153627558393996 1 Bob 3 3 0 100 2015-09-23 11:27:02
My query looks like the following:
SELECT * FROM `entries`
WHERE `TP_ID` IN('10153627558393996', '10154312970149546')
GROUP BY `TP_ID`
ORDER BY `correct` DESC
In my mind, what that should do is get the two users from the IN clause, order them by the number of correct answers and then group them together, so I should be left with the 2 highest scores from those two users.
In reality it's giving me two results, but the one from Joe gives me the lower of the two values (2), with Bob first with a score of 3. Swapping to ASC ordering keeps the scores the same but places Joe first.
So, how could I achieve what I need?
You're after the groupwise maximum, which can be obtained by joining the grouped results back to the table:
SELECT * FROM entries NATURAL JOIN (
SELECT TP_ID, MAX(correct) correct
FROM entries
WHERE TP_ID IN ('10153627558393996', '10154312970149546')
GROUP BY TP_ID
) t
Of course, if a user has multiple records with the maximal score, it will return all of them; should you only want some subset, you'll need to express the logic for determining which.
MySql is quite lax when it comes to group-by-clauses - but as a rule of thumb you should try to follow the rule that other DBMSs enforce:
In a group-by-query each column should either be part of the group-by-clause or contain a column-function.
For your query I would suggest:
SELECT `TP_ID`,`name`,max(`correct`) FROM `entries`
WHERE `TP_ID` IN('10153627558393996', '10154312970149546')
GROUP BY `TP_ID`,`name`
Since your table seems quite denormalized the group by name-par could be omitted, but it might be necessary in other cases.
ORDER BY is only used to specify in which order the results are returned but does nothing about what results are returned - so you need to apply the max()-function to get the highest number of right answers.

specific ordering in mysql

I have a sql statement that brings back ids. Currently I am ordering the id's with the usual "ORDER BY id". What I need to be able to do is have the query order the first 3 rows by specific id's that I set. The order the remaining as it is currently. For example, I want to say the first 3 rows will be id's 7,10,3 in that order, then the rest of the rows will be ordered by the id as usual.
right now i just have a basic sql statement...
SELECT * from cards ORDER BY card_id
SELECT *
FROM cards
ORDER BY
CASE card_id WHEN 7 THEN 1 WHEN 10 THEN 2 WHEN 3 THEN 3 ELSE 4 END,
card_id
A bit shorter than Quassnoi's query, with FIELD :
-- ...
ORDER BY FIELD(card_id, 3, 10, 7) DESC
You have to invert the order because of the DESC, I didn't find a way to do it more naturally.