Jump to content

count rows from one table with the info from a other table


Recommended Posts

Dear all, 

I want to count rows from a table with the info from a other table

Table 1: 

id = 22 + published = 1
id = 23 + published = 0
id = 24 + published = 1
id = 25 + published = 1

Table 2:

id = 22 + Car = A
id = 23 + Car = B
id = 24 + Car = C
id = 25 + Car = A

Result :

Car A = 2
Car B = 0
Car C = 1

who can help me with some code to come to a solution to do this.

Grtz Manfred

mysql> select * from table1;
+----+-----------+
| id | published |
+----+-----------+
| 22 |         1 |
| 23 |         0 |
| 24 |         1 |
| 25 |         1 |
+----+-----------+
4 rows in set (0.00 sec)

mysql> select * from table2;
+----+------+
| id | car  |
+----+------+
| 22 | A    |
| 23 | B    |
| 24 | C    |
| 25 | A    |
+----+------+
4 rows in set (0.03 sec)

mysql> SELECT car
    ->      , sum(published) as tot
    -> FROM table1
    ->      JOIN table2 USING (id)
    -> GROUP BY car;
+------+------+
| car  | tot  |
+------+------+
| A    |    2 |
| B    |    0 |
| C    |    1 |
+------+------+
3 rows in set (0.00 sec)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.