Reputation: 161
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
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
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
Reputation:
It will be like below :
Select (select count(colum_nname) from table2 ) as totalvalues from table
Upvotes: 0