DaHaKa
DaHaKa

Reputation: 161

mysql count function

I'm interested in how I can write mysql query if I want to count some column from table (mysql database) where that column must be chosen within another query ? like this SELECT COUNT(OTHER_QUERY) FROM TABLE

I have column named port and want to count this column within another query like this (select port where port='GCP';

I'm trying to do this task but with no result (despite of helping) I'm writing like so => SELECT COUNT(port) from data WHERE port = (select port from data where port='GCP') So I want to count column port which are equal to GCP

please help with this task :)

Upvotes: 0

Views: 636

Answers (3)

Ritwik
Ritwik

Reputation: 1

Do you want something like this :

Want total number of ports if it's value is GCP.

If so, then this is the syntax:

SELECT COUNT(port) AS alias WHERE port = GCP;

You may find this MySQL COUNT resources useful.

Upvotes: 0

bhamby
bhamby

Reputation: 15469

Just wrap the original query in a sub-select:

SELECT COUNT(A.column)
FROM (other_query) A

For your second query, I believe this should get what you want:

SELECT COUNT(A.column)
FROM (other_query) A
WHERE A.port='GCP'

Upvotes: 5

user1119566
user1119566

Reputation:

It will be like below :

Select (select count(colum_nname) from table2 )  as totalvalues from table

Upvotes: 0

Related Questions