Compare COUNT of two tables

7

I have two tables and should I return the table total with more data, I'm doing this:

SELECT CASE WHEN ((select count(*) as 'familiar' from tb.familiar)) > 
                 ((select count(*) as 'personal' from tb.personal))
                  THEN 0 ELSE 1 END AS total

That way it works, when the familiar table is greater it returns 0 and when it does not return 1. But how do I return the value of COUNT ? Because when I put the nicknames ( familiar or personal ) of the count gives syntax error, for example:

SELECT CASE WHEN ((select count(*) as 'familiar' from tb.familiar)) > 
                 ((select count(*) as 'personal' from tb.personal))
                  THEN familiar ELSE personal END AS total

Give the following error:

  

Unknown column 'familiar' in 'field list'

    
asked by anonymous 19.10.2016 / 17:54

1 answer

6

You would have to repeat the entire code of the two Select count... within THEN and ELSE .

Or ...

You can do a subselect:

SELECT 
    CASE WHEN FAMILIAR > PERSONAL THEN FAMILIAR ELSE PERSONAL END AS Total
FROM
(
    SELECT 
        (select count(*) from tb.familiar) AS FAMILIAR,
        (select count(*) from tb.personal) AS PERSONAL
) X
    
19.10.2016 / 18:12