Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do sql loop

Tags:

sql

mysql

Table works(emNo, comNo, salary).

I can get distinct comNo using "select distinct comNo from works". Suppose it gives me a column with 5 rows. How do I count "emNo`" for each of those rows?

like image 792
LynAs Avatar asked Nov 26 '25 16:11

LynAs


1 Answers

You can use GROUP BY to aggregate per type of comNo.

SELECT
  comNo,
  count(emNo)
FROM
  works
GROUP BY
  comNo

This will return one row per distinct value of comNo along with the count of records per group.

Demo: http://www.sqlfiddle.com/#!2/4f5df/1

like image 187
mellamokb Avatar answered Nov 28 '25 05:11

mellamokb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!