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
  • @asawyer I've looked it up and I can not figure out how to use it to get the results I'm looking for. If you could give an example query that would be great. Thanks
    – d_boggus
    Apr 20, 2012 at 16:09

1 Answer 1

0

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)
1
  • Wow, its really quite complicated but that worked for me. Now I just have to translate this over to my actual tables. Thanks.
    – d_boggus
    Apr 20, 2012 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.