erhanelu ion erhanelu ion - 5 months ago 7
MySQL Question

Create a count in count in sql select

I have a question: it's possible to create an count in count in sql:
my code is:

SELECT COUNT( DISTINCT p.id_participant ) as number
FROM participation p
INNER JOIN message m ON m.id_participation=p.id
AND p.id_event = 4


I want to add in first count another count from table winners with count (id_winner)
Help me please, Exist a solution?

Answer

You need to use the aggregate function SUM.

For example,

SQL> SELECT SUM(val)
  2  FROM  (SELECT Count(*) VAL
  3         FROM   emp
  4         UNION
  5         SELECT Count(*) VAL
  6         FROM   dept);

  SUM(VAL)
----------
        18