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 7820493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:19:05+00:00 2026-06-02T07:19:05+00:00

This question has been a little difficult for me to formulate but I think

  • 0

This question has been a little difficult for me to formulate but I think I have come up with an example that will get the idea across.

I have three tables bird_brd, state_stt, and bird_in_state_bis

The first two tables are just lists and the third table is a way to join the other two togther and has a count field for the number of times a bird has been seen in a state.

I don’t think the structure of the tables matters too much but here are the basics

bird_brd
=====================
| id_brd | name_brd |
=====================
|    1   | Blue Jay |
|    2   |  Robbin  |
=====================

state_stt
=====================
| id_stt | name_stt |
=====================
|   1    |   Utah   |
|   2    |  Arizona |
|   3    |  Wyoming |
=====================

bird_in_state_bis
=======================================
| stt_id_bis | brd_id_bis | count_bis |
=======================================
|     1      |      1     |     5     |
|     2      |      2     |     3     |
=======================================

What I want to be able to do is combine these tables so that if there is an entry in the bird_in_state table for a state it will show the count for all birds whether they are in the bird_in_state table or not.

so when I run the query I would expect results like the following

==================================
| State Name | Bird Name | Count |
==================================
|    Utah    | Blue Jay  |   5   |
|    Utah    |  Robbin   |   0   |
|   Arizona  | Blue Jay  |   0   |
|   Arizona  |  Robbin   |   3   |
==================================

Note that this is something I’m trying to do for work and the above tables are not the actual tables I’m working with but they are a good example of the structure I have to work with.

I have tried to use some sort of left or right join but I don’t get what I’m looking for.

Thanks in advance for any help.

  • 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-06-02T07:19:07+00:00Added an answer on June 2, 2026 at 7:19 am

    I think this is what you’re looking for.

    SELECT 
      Bird.Name, 
      State.Name, 
      IFNULL(BirdsInState.Count, 0) AS Count 
    FROM (SELECT State AS StateID,ID AS BirdID FROM (SELECT DISTINCT(State) AS State FROM BirdsInState) AS UniqStates, Bird) BirdStates 
    LEFT JOIN Bird ON BirdStates.BirdID = Bird.ID 
    LEFT JOIN State ON BirdStates.StateID = State.ID 
    LEFT JOIN BirdsInState ON BirdID = BirdsInState.Bird 
      AND StateID = BirdsInState.State;
    

    It first does the Cartesian Join across all used States and all available birds to get all possible combinations. Then it joins back to the original three tables to get the names of the birds, states, and counts. In such a join, the count would be null if it weren’t available, so we use the IFNULL function to convert all nulls to 0’s.

    Where I’m using these tables:

    mysql> SHOW CREATE TABLE State;
    +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table                                                                                                                                                                   |
    +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | State | CREATE TABLE `State` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `Name` varchar(63) DEFAULT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 |
    +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SHOW CREATE TABLE Bird;
    +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table                                                                                                                                                                  |
    +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Bird  | CREATE TABLE `Bird` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `Name` varchar(63) DEFAULT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 |
    +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SHOW CREATE TABLE BirdsInState;
    +--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table        | Create Table                                                                                                                                                                                                                                |
    +--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | BirdsInState | CREATE TABLE `BirdsInState` (
      `Bird` int(11) NOT NULL DEFAULT '0',
      `State` int(11) NOT NULL DEFAULT '0',
      `Count` int(11) DEFAULT NULL,
      PRIMARY KEY (`Bird`,`State`),
      KEY `State` (`State`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
    +--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row 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 know that this question have been asked several times. But I can't get
This question has been asked before ( link ) but I have slightly different
This question has been asked before, but I would like a little more detail
I know this question has been possed before, but the explanation was a little
I'm sure this question has been answered before, but I don't think I'm using
This question has been brought up before, and I have searched many of the
This question has been asked before but with no answer. I'm running into brick
This question has been asked in a C++ context but I'm curious about Java.
This question has been puzzling me for a long time now. I come from
This question has been asked before but the answers aren't always clear or are

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.