I have two tables in a MySQL database. The first one has a list of department names.
departments
abbreviation | name
-------------|-------------
Try this
SELECT d.abbreviation, COUNT(*) num
FROM departments d
INNER JOIN courses c ON c.section LIKE CONCAT(d.abbreviation, "%")
GROUP BY d.abbreviation
SELECT d.abbreviation, COUNT(*) num
FROM departments d
INNER JOIN courses c ON c.section LIKE CONCAT(d.abbreviation, "%")
GROUP BY d.abbreviation
Sql Fiddle
I quick solution, but not the best, could be:
SELECT abbreviation,
(SELECT COUNT(*) FROM courses C WHERE D.abbreviation = SUBSTRING(C.abbreviation, 0, 3)) AS c
FROM departments D;
Give this a shot:
select
d.abbreviation,
count(d.abbreviation) count
from
departments d
inner join
courses c
on (LOCATE(d.abbreviation,c.section) <> 0)
group by d.abbreviation;