Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 962653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:32:32+00:00 2026-05-16T01:32:32+00:00

I have two tables, foo and bar : +—-+—–+ +—-+—–+ | id | val

  • 0

I have two tables, foo and bar:

+----+-----+    +----+-----+
| id | val |    | id | val |
+----+-----+    +----+-----+
|  1 | qwe |    |  1 | asd |
|  2 | rty |    |  3 | fgh |
+----+-----+    +----+-----+

id is not unique here. Not all IDs in foo have their equivalents in bar and vice versa. I need to count all rows with specific ID in both tables and present them in a new table, e.g.:

+----+-----------+-----------+
| id | count_foo | count_bar |
+----+-----------+-----------+
|  1 |         1 |         1 |
|  2 |         1 |         0 |
|  3 |         0 |         1 |
+----+-----------+-----------+

I’ve tried UNION SELECT:

SELECT id, COUNT(id) AS count_foo, 0 AS count_bar FROM foo GROUP BY id
UNION SELECT id, 0, COUNT(id) FROM bar GROUP BY id;

But this outputs row with id=1 twice, like

+----+-----------+-----------+
| id | count_foo | count_bar |
+----+-----------+-----------+
|  1 |         1 |         0 |  <- not good
|  2 |         1 |         0 |
|  1 |         0 |         1 |  <- not good
|  3 |         0 |         1 |
+----+-----------+-----------+

I’ve also tried LEFT JOIN:

SELECT id, COUNT(foo.id) AS count_foo, COUNT(bar.id) AS count_bar
FROM foo LEFT JOIN bar USING(id) GROUP BY id;

But this query ignores rows from table bar with ID that is missing in table foo:

+----+-----------+-----------+
| id | count_foo | count_bar |
+----+-----------+-----------+
|  1 |         1 |         1 |
|  2 |         1 |         0 |
+----+-----------+-----------+  <- can I haz `id=3`?

What am I missing? What would be the right query or the right manual to read?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:32:33+00:00Added an answer on May 16, 2026 at 1:32 am

    You may want to try the following:

    SELECT d.id,
           (SELECT COUNT(*) FROM foo WHERE id = d.id) count_foo,
           (SELECT COUNT(*) FROM bar WHERE id = d.id) count_bar
    FROM   ((SELECT id FROM foo) UNION (SELECT id FROM bar)) d;
    

    Test case:

    CREATE TABLE foo (id int, val varchar(5));
    CREATE TABLE bar (id int, val varchar(5));
    
    INSERT INTO foo VALUES (1, 'qwe');
    INSERT INTO foo VALUES (2, 'rty');
    INSERT INTO bar VALUES (1, 'asf');
    INSERT INTO bar VALUES (3, 'ghj');
    

    Result:

    +------+-----------+-----------+
    | id   | count_foo | count_bar |
    +------+-----------+-----------+
    |    1 |         1 |         1 |
    |    2 |         1 |         0 |
    |    3 |         0 |         1 |
    +------+-----------+-----------+
    3 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables named foo and bar , hypothetically speaking. foo has columns
I have two tables, we'll call them Foo and Bar , with a one
I have two tables, foo and bar , and I want foo.bar_id to link
I have two MySQL tables, here's how they are structured: table foo( foo_id varchar(32),
I have two classes Foo and Bar mapped to two different tables, and I'd
Suppose I have a database table with two fields, foo and bar. Neither of
Having difficulty articulating this correlated subquery. I have two tables fictitious tables, foo and
I have two tables that are joined together. A has many B Normally you
I have two tables, Book and Tag , and books are tagged using the
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.