Get the greatest number of the products - mysql

The goal
I want to make a ranking and I need to get the 10 firsts of a column.
The problem
There's the following table within my application:
+------+---------+
| User | Product |
+------+---------+
| 1 | 1 |
+------+---------+
| 2 | 1 |
+------+---------+
| 3 | 1 |
+------+---------+
| 4 | 1 |
+------+---------+
| 5 | 2 |
+------+---------+
| 6 | 2 |
+------+---------+
| 7 | 2 |
+------+---------+
| 8 | 3 |
+------+---------+
| 9 | 3 |
+------+---------+
And I want to make a ranking following this pattern:
+---------+----------+
| Product | Quantity |
+---------+----------+
| 1 | 4 |
+---------+----------+
| 2 | 3 |
+---------+----------+
| 3 | 2 |
+---------+----------+
How can I do this?

You can do this:
SELECT product, COUNT(product)
FROM yourtable
GROUP BY product
ORDER BY COUNT(product) DESC
LIMIT 10;

Something like this should do it.
SELECT TOP 10 Product, COUNT(*)
FROM Table
GROUP BY Product
ORDER BY COUNT(*) DESC;

Related

Select distinct and count number of rows

I have a table with this structure
+----------+----------+
| user_id | tema_id |
+----------+----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 2 |
| 6 | 3 |
| 7 | 1 |
+----------+----------+
What I want to get in only one query is the total of different tema_id by tema_id. I have this query but it returns the different tema_id but the count column to one instead of the total of that tema_id.
SELECT tema_id, COUNT(DISTINCT(tema_id)) as total
FROM push_subscriptions
GROUP BY tema_id
Return this:
+----------+----------+
| tema_id | total |
+----------+----------+
| 1 | 1 | -> must be 3
| 2 | 1 | -> must be 2
| 3 | 1 | -> must be 2
+----------+----------+
Thank you
A simple count(*) should do the trick:
select tema_id, count(*) as total
from push_subscriptions
group by tema_id;
Fiddle

MYSQL group by and counting columns

Say I have a table like this:
+--------+-------+
| ID | LETTER|
+--------+-------+
| 1 | A |
| 1 | A |
| 1 | A |
| 2 | A |
| 2 | B |
| 3 | C |
| 3 | D |
| 4 | D |
+--------+-------+
How would I implement a query that returns the ID followed by how many unique letters it has? So it would look like this:
+--------+-------+
| ID | LETTER|
+--------+-------+
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
+--------+-------+
I've tried using group by but I cant figure it out.
You should search for the answer before questioning. But here is what you need:
SELECT yt.ID, COUNT(DISTINCT yt.LETTER)
FROM yourtable yt
GROUP BY yt.ID

Join 2 tables with distinct

I have 2 tables, follow and following
follow
+---------+----------------+
| user_id | follow_user_id |
+---------+----------------+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 100 | 10 |
+---------+----------------+
following
+---------+-------------------+
| user_id | following_user_id |
+---------+-------------------+
| 1 | 2 |
| 3 | 4 |
| 4 | 6 |
| 200 | 500 |
+---------+-------------------+
I want to concat 2 tables without duplicate.
Here is the result that I want.
+---------+----------------+-----------+
| user_id | target_user_id | category |
+---------+----------------+-----------+
| 1 | 2 | follow |
| 2 | 3 | follow |
| 3 | 4 | follow |
| 4 | 6 | following |
| 100 | 10 | follow |
| 200 | 500 | following |
+---------+----------------+-----------+
Condition 1 - Remove duplicated row
Condition 2 - Have to add category column with each table's name
Condition 3 - If category is duplicated, it can be follow or following. it doesn't matter.
Condition 4 - follow_user_id as target_user_id and following_user_id as target_user_id
In this case, should I have to use join? or union?
Any suggestion, very appreciate.
Thanks!
Just use union and group by, the SQL as below:
select
user_id,target_user_id,min(tag) as category
from
(
select user_id,follow_user_id as target_user_id, 'follow' as tag from follow
union
select user_id,following_user_id as target_user_id, 'following' as tag from following
) tmp
group by
user_id,target_user_id
order by
user_id,target_user_id;
+---------+----------------+-----------+
| user_id | target_user_id | category |
+---------+----------------+-----------+
| 1 | 2 | follow |
| 2 | 3 | follow |
| 3 | 4 | follow |
| 4 | 6 | following |
| 100 | 10 | follow |
| 200 | 500 | following |
+---------+----------------+-----------+
6 rows in set (0.00 sec)

MySQL select two smallest id with duplicated conditions

So i have a table room like this
+---------+-------------+
| room_id | status |
+---------+-------------+
| 1 | unavailable |
| 2 | available |
| 3 | unavailable |
| 4 | available |
| 5 | available |
+---------+-------------+
if I use
SELECT * FROM room WHERE status='available'
the output of course will be
+---------+-----------+
| room_id | status |
+---------+-----------+
| 2 | available |
| 4 | available |
| 5 | available |
+---------+-----------+
But i want the output only two of the smallest id like this
+---------+-----------+
| room_id | status |
+---------+-----------+
| 2 | available |
| 4 | available |
+---------+-----------+
Can you guys help me with this?thanks
You can try below - using order by and limit
SELECT * FROM room WHERE status='available'
order by room_id asc
limit 2

Mysql : select count when id have multiple same value

+------+---------+
| id | object |
+------+---------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 3 | 5 |
| 3 | 3 |
| 3 | 4 |
+------+---------+
i want to select count id where have a same value, so the result be, id 1 have 4 same value, id 2 have 2 same value, id 3 have 3 same value .
+------+
| id |
+------+
| 4 |
| 2 |
| 3 |
+------+
thanks for help, master.
SELECT id, COUNT(object) FROM tablename GROUP BY id
SELECT COUNT( * ) FROM `test` GROUP BY id