我想这就是你要找的。
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;
它首先在所有使用的州和所有可用的鸟类中进行笛卡尔连接,以获得所有可能的组合。然后它连接回原来的三个表以获取鸟类的名称、状态和计数。在这样的连接中,如果不可用,计数将为空,因此我们使用该IFNULL
函数将所有空值转换为 0。
我在哪里使用这些表:
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)