Showing colum on different table with count sql

36
October 25, 2019, at 4:10 PM

I have 2 tables.

tbl_don
- id_donatur
- id_program
- ...

and

tbldonasi
- id_donatur
- id_program
- ...

I want to show following data from these tables.

| id_program | id_donatur |
---------------------------
| 1          | 2          |
| 2          | 3          |

where the id_donatur column is counted from table 1 and 2

I have run this query

select A.id_donatur, A.id_program from tbl_don A UNION all select B.id_donatur, B.id_program from tbldonasi B

But I am confused how to completed this query.

Answer 1

You have to use a group by clause on the union. Something similar to below:

select U.id_program, count(U.id_donatur) 
from 
   (select A.id_donatur, A.id_program 
    from tbl_don A 
    UNION all 
    select B.id_donatur, B.id_program 
    from tbldonasi B) AS U 
GROUP BY U.id_program
READ ALSO
Derive new column of weekly unique users alongside daily unique users in MySQL

Derive new column of weekly unique users alongside daily unique users in MySQL

I have a table where I have daily unique users in the month of JanuaryI am trying to derive a new column (in the same table) that shows total weekly unique users

27
Laravel PDOException(code: 0) failed loading cafile stream

Laravel PDOException(code: 0) failed loading cafile stream

I get this error when I try to perform a DB operation from the public siteThis is from a server that connects remotely to a dedicated database server

34