VVB
VVB

Reputation: 7641

How to substract from count of 2 results in mysql query

I am new to mysql. I have 2 seperate queries which give me counts.

I want to get [COUNT 1 - COUNT 2]. But, I don't know how to do it in mysql.

Below is my query

(select COUNT(CITY) as M from STATION) 
MINUS
(select COUNT(DISTINCT CITY) as N from STATION) 

Error

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-
(select COUNT(DISTINCT CITY) as N from STATION)' at line 2

Upvotes: 1

Views: 105

Answers (3)

Usman
Usman

Reputation: 2029

Try this query !

SELECT COUNT(CITY)
- COUNT(DISTINCT CITY) AS 'M-N' FROM STATION

Upvotes: 0

VVB
VVB

Reputation: 7641

Solved

select ((select COUNT(CITY) as M from STATION) MINUS (select COUNT(DISTINCT CITY) as N from STATION)) as count from STATION LIMIT 1

Upvotes: 0

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

You can do it in single query like

SELECT COUNT(CITY) - COUNT(DISTINCT CITY)
FROM STATION

Upvotes: 1

Related Questions