How can I make groupwise count for each table and print them columnwise? [duplicate]












2
















This question already has an answer here:




  • How to do a FULL OUTER JOIN in MySQL?

    14 answers




Here I have two tables each have supervision field and user_id field. I want groupwise count for each supervision for both the table and print as below. both the tables contain different data.



Also count should be for specified user_id only.



Table1 columns



+----+-------------+---------+
| id | supervision | user_id |
+----+-------------+---------+
| 1 | type1 | 2 |
| 2 | type1 | 2 |
| 3 | type2 | 1 |
| 4 | type1 | 2 |
| 5 | type2 | 2 |
+----+-------------+---------+


Table2 columns



+----+-------------+---------+
| id | supervision | user_id |
+----+-------------+---------+
| 1 | type3 | 2 |
| 2 | type1 | 2 |
| 3 | type3 | 1 |
| 4 | type1 | 2 |
| 5 | type2 | 2 |
+----+-------------+---------+


For user_id=2 it should give output like this:



+-------+--------+--------+
| Type | table1 | table2 |
+-------+--------+--------+
| type1 | 3 | 2 |
| type2 | 1 | 1 |
| type3 | 0 | 1 |
| .... | | |
+-------+--------+--------+


For now this query I tried it gives correct result for table1 for table2 not.



select t1.supervision,
count(t1.supervision) AS table1,
count(t2.supervision) AS table2 from table1 t1
LEFT JOIN
table2 t2 ON t1.id=t2.id
where t1.userid=2 AND t2.userid=2
group by t1.supervision









share|improve this question















marked as duplicate by Barmar mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 16:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • MySQL or SQL Server? Please don't tag multiple database engines.

    – DavidG
    Nov 13 '18 at 16:09











  • Is it possible to have some type* values, which exist in Table 1 only, and not in Table2, and vice versa being true as well ?

    – Madhur Bhaiya
    Nov 13 '18 at 16:16
















2
















This question already has an answer here:




  • How to do a FULL OUTER JOIN in MySQL?

    14 answers




Here I have two tables each have supervision field and user_id field. I want groupwise count for each supervision for both the table and print as below. both the tables contain different data.



Also count should be for specified user_id only.



Table1 columns



+----+-------------+---------+
| id | supervision | user_id |
+----+-------------+---------+
| 1 | type1 | 2 |
| 2 | type1 | 2 |
| 3 | type2 | 1 |
| 4 | type1 | 2 |
| 5 | type2 | 2 |
+----+-------------+---------+


Table2 columns



+----+-------------+---------+
| id | supervision | user_id |
+----+-------------+---------+
| 1 | type3 | 2 |
| 2 | type1 | 2 |
| 3 | type3 | 1 |
| 4 | type1 | 2 |
| 5 | type2 | 2 |
+----+-------------+---------+


For user_id=2 it should give output like this:



+-------+--------+--------+
| Type | table1 | table2 |
+-------+--------+--------+
| type1 | 3 | 2 |
| type2 | 1 | 1 |
| type3 | 0 | 1 |
| .... | | |
+-------+--------+--------+


For now this query I tried it gives correct result for table1 for table2 not.



select t1.supervision,
count(t1.supervision) AS table1,
count(t2.supervision) AS table2 from table1 t1
LEFT JOIN
table2 t2 ON t1.id=t2.id
where t1.userid=2 AND t2.userid=2
group by t1.supervision









share|improve this question















marked as duplicate by Barmar mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 16:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • MySQL or SQL Server? Please don't tag multiple database engines.

    – DavidG
    Nov 13 '18 at 16:09











  • Is it possible to have some type* values, which exist in Table 1 only, and not in Table2, and vice versa being true as well ?

    – Madhur Bhaiya
    Nov 13 '18 at 16:16














2












2








2









This question already has an answer here:




  • How to do a FULL OUTER JOIN in MySQL?

    14 answers




Here I have two tables each have supervision field and user_id field. I want groupwise count for each supervision for both the table and print as below. both the tables contain different data.



Also count should be for specified user_id only.



Table1 columns



+----+-------------+---------+
| id | supervision | user_id |
+----+-------------+---------+
| 1 | type1 | 2 |
| 2 | type1 | 2 |
| 3 | type2 | 1 |
| 4 | type1 | 2 |
| 5 | type2 | 2 |
+----+-------------+---------+


Table2 columns



+----+-------------+---------+
| id | supervision | user_id |
+----+-------------+---------+
| 1 | type3 | 2 |
| 2 | type1 | 2 |
| 3 | type3 | 1 |
| 4 | type1 | 2 |
| 5 | type2 | 2 |
+----+-------------+---------+


For user_id=2 it should give output like this:



+-------+--------+--------+
| Type | table1 | table2 |
+-------+--------+--------+
| type1 | 3 | 2 |
| type2 | 1 | 1 |
| type3 | 0 | 1 |
| .... | | |
+-------+--------+--------+


For now this query I tried it gives correct result for table1 for table2 not.



select t1.supervision,
count(t1.supervision) AS table1,
count(t2.supervision) AS table2 from table1 t1
LEFT JOIN
table2 t2 ON t1.id=t2.id
where t1.userid=2 AND t2.userid=2
group by t1.supervision









share|improve this question

















This question already has an answer here:




  • How to do a FULL OUTER JOIN in MySQL?

    14 answers




Here I have two tables each have supervision field and user_id field. I want groupwise count for each supervision for both the table and print as below. both the tables contain different data.



Also count should be for specified user_id only.



Table1 columns



+----+-------------+---------+
| id | supervision | user_id |
+----+-------------+---------+
| 1 | type1 | 2 |
| 2 | type1 | 2 |
| 3 | type2 | 1 |
| 4 | type1 | 2 |
| 5 | type2 | 2 |
+----+-------------+---------+


Table2 columns



+----+-------------+---------+
| id | supervision | user_id |
+----+-------------+---------+
| 1 | type3 | 2 |
| 2 | type1 | 2 |
| 3 | type3 | 1 |
| 4 | type1 | 2 |
| 5 | type2 | 2 |
+----+-------------+---------+


For user_id=2 it should give output like this:



+-------+--------+--------+
| Type | table1 | table2 |
+-------+--------+--------+
| type1 | 3 | 2 |
| type2 | 1 | 1 |
| type3 | 0 | 1 |
| .... | | |
+-------+--------+--------+


For now this query I tried it gives correct result for table1 for table2 not.



select t1.supervision,
count(t1.supervision) AS table1,
count(t2.supervision) AS table2 from table1 t1
LEFT JOIN
table2 t2 ON t1.id=t2.id
where t1.userid=2 AND t2.userid=2
group by t1.supervision




This question already has an answer here:




  • How to do a FULL OUTER JOIN in MySQL?

    14 answers








mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 16:11









Madhur Bhaiya

19.6k62236




19.6k62236










asked Nov 13 '18 at 16:08









Vipul M.Vipul M.

112




112




marked as duplicate by Barmar mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 16:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Barmar mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 16:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • MySQL or SQL Server? Please don't tag multiple database engines.

    – DavidG
    Nov 13 '18 at 16:09











  • Is it possible to have some type* values, which exist in Table 1 only, and not in Table2, and vice versa being true as well ?

    – Madhur Bhaiya
    Nov 13 '18 at 16:16



















  • MySQL or SQL Server? Please don't tag multiple database engines.

    – DavidG
    Nov 13 '18 at 16:09











  • Is it possible to have some type* values, which exist in Table 1 only, and not in Table2, and vice versa being true as well ?

    – Madhur Bhaiya
    Nov 13 '18 at 16:16

















MySQL or SQL Server? Please don't tag multiple database engines.

– DavidG
Nov 13 '18 at 16:09





MySQL or SQL Server? Please don't tag multiple database engines.

– DavidG
Nov 13 '18 at 16:09













Is it possible to have some type* values, which exist in Table 1 only, and not in Table2, and vice versa being true as well ?

– Madhur Bhaiya
Nov 13 '18 at 16:16





Is it possible to have some type* values, which exist in Table 1 only, and not in Table2, and vice versa being true as well ?

– Madhur Bhaiya
Nov 13 '18 at 16:16












1 Answer
1






active

oldest

votes


















0














SELECT t1.supervision, count(t1.id) AS table1, count(t2.id) AS table2
FROM users u
LEFT JOIN table1 t1 on (t1.user_id = u.id)
LEFT JOIN table2 t2 on (t2.user_id = u.id)
WHERE u.id = 2
GROUP BY t1.supervision


This assumes the users table exists. The problem you had was type3 did not exits in t1 so the left join would not have included values from there, and you were joining on id instead of user_id.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    SELECT t1.supervision, count(t1.id) AS table1, count(t2.id) AS table2
    FROM users u
    LEFT JOIN table1 t1 on (t1.user_id = u.id)
    LEFT JOIN table2 t2 on (t2.user_id = u.id)
    WHERE u.id = 2
    GROUP BY t1.supervision


    This assumes the users table exists. The problem you had was type3 did not exits in t1 so the left join would not have included values from there, and you were joining on id instead of user_id.






    share|improve this answer




























      0














      SELECT t1.supervision, count(t1.id) AS table1, count(t2.id) AS table2
      FROM users u
      LEFT JOIN table1 t1 on (t1.user_id = u.id)
      LEFT JOIN table2 t2 on (t2.user_id = u.id)
      WHERE u.id = 2
      GROUP BY t1.supervision


      This assumes the users table exists. The problem you had was type3 did not exits in t1 so the left join would not have included values from there, and you were joining on id instead of user_id.






      share|improve this answer


























        0












        0








        0







        SELECT t1.supervision, count(t1.id) AS table1, count(t2.id) AS table2
        FROM users u
        LEFT JOIN table1 t1 on (t1.user_id = u.id)
        LEFT JOIN table2 t2 on (t2.user_id = u.id)
        WHERE u.id = 2
        GROUP BY t1.supervision


        This assumes the users table exists. The problem you had was type3 did not exits in t1 so the left join would not have included values from there, and you were joining on id instead of user_id.






        share|improve this answer













        SELECT t1.supervision, count(t1.id) AS table1, count(t2.id) AS table2
        FROM users u
        LEFT JOIN table1 t1 on (t1.user_id = u.id)
        LEFT JOIN table2 t2 on (t2.user_id = u.id)
        WHERE u.id = 2
        GROUP BY t1.supervision


        This assumes the users table exists. The problem you had was type3 did not exits in t1 so the left join would not have included values from there, and you were joining on id instead of user_id.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 16:21









        Nick EllisNick Ellis

        689718




        689718















            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values