Already checked other answers but unfortunately didn’t help me. So im hoping to count how many same id from different table. This is how my tables look like
The first table from the left is “resources” and the second is “equipment_entity”. So I want to count how many resource_class_id
is there when joined with equipment_entity.id
. This is what I’ve tried so far but doesn’t work. Any help would do. Link with same problem or any help. Thanks ahead!
SELECT e.*, r.equipment_class_id, r.resource_type FROM equipment_entity as e LEFT JOIN resources as r ON e.id = r.equipment_class_id COUNT(DISTINCT r.equipment_class_id) where r.resource_type = 'equipment'
and also, im having this error
1064 – You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near ‘COUNT(DISTINCT r.equipment_class_id) where r.resource_type =
‘equipment’ LIMIT 0′ at line 1
4
Answers
Try following query. This will give you all the Rows from equipment_entity Table and the count of rows in resource table for each equipment.
Use this
There are two options. You you are decided that you wants to know the count for r.resource_type = ‘equipment’, then the first query will work for you.
But if you wants to see all resource_type’s count in one go, you can use GROUP BY as below to get your desired results.
I don’t quite follow that this means. Two interpretations are below.
Based on your data model, the table
resources
seems to have at least two columnsid
andresource_class
. The tableequipment_entity
seems to haveresource_class_id
. I have adjusted this answer to reflect these names.If you want to count the number of equipment that have a particular resource type, then just use
JOIN
andCOUNT
():Neither
COUNT(DISTINCT)
nor aLEFT JOIN
are needed.If you want to count all resource classes, then: