Mysql : select count when id have multiple same value - mysql

+------+---------+
| 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

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

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)

SQL Distinct a column with conditions

I'm working on a query where I need to count distinct CarId row when the column LocationId is not null and get all CarId if its null or 0 but the query that I tried distincts all the CarId even if its null
#LocId int
Select Count(distinct a.CarId) from VehicleDetails a
inner join VehicleDocuments b on a.DocId=b.DocId
left join VehicleShipmentDetails dpg on dpg.VehicleShipmentId= b.VehicleShipmentId
where b.LogicalDelete=0 and a.LogicalDelete=0
and (dpg.LocationId= #LocId or dpg.LocationId= 0 or dpg.LocationId is null)
| ID | CarId | LocationId | DateCreated |
|------+----------------+-----------------+---------------|
| 1 | 1 | 5 | 02/03/2019 |
| 2 | 2 | null | 01/14/2019 |
| 3 | 2 | 0 | 02/03/2019 |
| 4 | 2 | 5 | 12/30/2018 |
| 5 | 4 | 3 | 01/10/2019 |
| 6 | 3 | 5 | 02/14/2019 |
| 7 | 2 | 5 | 03/13/2019 |
Desired output:
| ID | CarId | LocationId | DateCreated |
+------+----------------+-----------------+---------------+
| 1 | 1 | 5 | 02/03/2019 |
| 2 | 2 | null | 01/14/2019 |
| 3 | 2 | 0 | 02/03/2019 |
| 4 | 2 | 5 | 03/13/2019 |
| 5 | 4 | 3 | 01/10/2019 |
| 6 | 3 | 5 | 02/14/2019 |
Current Output
| ID | CarId | LocationId | DateCreated |
+------+----------------+-----------------+---------------+
| 1 | 1 | 5 | 02/03/2019 |
| 2 | 2 | 5 | 01/14/2019 |
| 3 | 4 | 3 | 01/10/2019 |
| 4 | 3 | 5 | 02/14/2019 |
Im getting a count of 4 but i needed to have 6 as the Count
EDIT: My goal is to remove the row to Distinct CarId if the value of the LocationId is Null or 0 but on my Current code, It distincts all CarId that is null,0 and equals to #LocId
You can query something like this, replace your_table by your actual set of data.
SELECT ID, CardId, LocationId, DateCreated
FROM your_table as T
WHERE NOT EXISTS (SELECT *
FROM your_table as T1
WHERE T.ID > T1.ID AND T.CarID = T1.CarID)
In SQL, you can use the statement CASE to manage conditions (just like the "if then else" in other programming languages). In your case this function could help because you have two differents cases to handle.

How to select related row when current row is empty?

I have a table like this:
// mytable
+----+---------+---------+
| id | name | related |
+----+---------+---------+
| 1 | Jack | 1 |
| 2 | | 1 |
| 3 | | 1 |
| 4 | | 2 |
| 5 | peter | 2 |
| 6 | peter | 2 |
| 7 | | 2 |
| 8 | jhon | 4 |
| 9 | | 3 |
| 19 | ali | 3 |
| 20 | | 4 |
| 21 | | 4 |
+----+---------+---------+
All I have is a id-number, Here is my query:
SELECT name FROM mytable WHERE id = :id LIMIT 1
In my query sometimes name is empty. So I'm trying to select related name, how can I do that?
Here is some example: (plus expected output)
:id = 1
+---------+
| Jack |
+---------+
:id = 2
+---------+
| Jack |
+---------+
:id = 21
+---------+
| jhon |
+---------+
:id = 6
+---------+
| peter |
+---------+
The query below will work with ids 1, 2 and 6. I'm not sure how to get id 21 to equal jhon. If I join the table four times I get to Jack. I hope this helps.
SELECT (CASE WHEN A.name IS NULL THEN B.name ELSE A.name END)
FROM mytable A LEFT JOIN mytable B ON (A.related=B.id)

How can I merge and count MySQL rows that are identical except for an ID column?

I want to be able to merge rows and find out how many are the same excluding the ID. For example, if I was to have this table:
+---------+-----------+-----------+
| ID | Col 1 | Col 2 |
+---------+-----------+ ----------+
| 1 | 1 | 5 |
| 2 | 1 | 5 |
| 3 | 4 | 9 |
| 4 | 3 | 9 |
| 5 | 1 | 5 |
| 6 | 1 | 5 |
| 7 | 1 | 5 |
| 8 | 4 | 9 |
+---------+-----------+-----------+
It would become:
+---------+-----------+-----------+---------+
| ID | Col 1 | Col 2 | Count |
+---------+-----------+ ----------+---------+
| 1 | 1 | 5 | 5 |
| 2 | 4 | 9 | 2 |
| 3 | 3 | 9 | 1 |
+---------+-----------+-----------+---------+
What would the query for this be?
beside the ID column in your result, to me it looks like you need:
SELECT Col1, Col2, Count(Col1)
FROM myTable
GROUP BY Col1, Col2
select col1,col2,count(*) as `count`
from table
group by col1,col2