COUNT in several tables

Asked

Viewed 853 times

1

I have two tables in my system, and I need to count two columns in the tables. I currently do through a VIEW as follows:

CREATE VIEW totais AS
(SELECT (SELECT COUNT(publications.id) FROM publications) AS total1,
(SELECT COUNT(deejays.id) FROM deejays) AS total2)

I need to do it through a SELECT but I’m not getting it.

  • 1

    And why can’t you use the VIEW? I don’t know if you noticed, she’s a SELECT.

  • 1

    @mustache SELECT COUNT(publications.id) as total1, COUNT(deejays.id) as total2 from publications, deejays wouldn’t work?

  • 2

    @R.Santos I don’t see why it wouldn’t work ;)

2 answers

3


You can do it like this:

SELECT
  (SELECT COUNT(publications.id) FROM publications) AS total1,
  (SELECT COUNT(deejays.id) FROM deejays) AS total2)

0

One way that could be done is also the following:

SELECT COUNT(publications.id) as total1, 
COUNT(deejays.id) as total2 
from publications, deejays
  • the results are equal in both variables. type Count is equal in both.

  • @Fláviokowalske, How so?

  • in one table I have 10 records and in the other 4. when I rotate the query returns 40 in both variables.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.