1

SELECT reporting.station FROM reporting LEFT JOIN channel ON MD5(igmpurl)=reporting.station WHERE reporting.station!="n/a" ORDER BY name;

resulting in this table:

Query like it is

Now I'd like to count the number of each element, which should something look like:

Result like I'd would like to have it

Note: I know about COUNT(station) which would return the number of the rows (20) but is not what I want.

Any idea how to solve this in MySQL (InnoDB)? Many thanks and regards

2 Answers 2

3
SELECT
    reporting.station, COUNT(reporting.station)
FROM
    reporting
LEFT JOIN
    channel
ON
    MD5(igmpurl)=reporting.station
WHERE
    reporting.station!="n/a"
GROUP BY reporting.station
ORDER BY
    name;

Try this.

Sign up to request clarification or add additional context in comments.

Comments

1

Use COUNT() and GROUP BY:

In your case, it should be something like this:

SELECT
    reporting.station,
    COUNT(*) AS rep_station_count
FROM
    reporting
LEFT JOIN
    channel
ON
    MD5(igmpurl)=reporting.station
WHERE
    reporting.station!="n/a"
GROUP BY
    reporting.station
ORDER BY
    name;

Note that allowing normal and aggregate columns (e.g. AVG(),COUNT() etc.) in the same query is MySQL specific.

3 Comments

(also, I'm not quite sure what you need the ORDER BY for, but that's not directly related to your question)
Thanks to you too. I'll give Headshota the accept because he was 2 minutes faster :P (and he needs the reputation more than you do :P) ORDER BY isn't that important. May I somehow get the row with the highest count? Or is the easiest way to ORDER BY the count and take the first?
@Atmocreations: For getting the highest count, you're on the right track: in this case, it would be ORDER BY rep_station_count DESC - first row is the one you want. Easiest and probably fastest, too :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.