这个问题对我来说有点难以制定,但我想我已经想出了一个可以实现这个想法的例子。
我有三张桌子bird_brd,state_stt和bird_in_state_bis
前两个表只是列表,第三个表是加入另外两个表的方法,并且有一个计数字段表示在一个州看到一只鸟的次数。
我不认为表格的结构太重要,但这里是基础
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 |
=======================================
我希望能够做的是组合这些表,以便如果bird_in_state表中有一个状态的条目,它将显示所有鸟类的计数,无论它们是否在bird_in_state表中。
所以当我运行查询时,我希望得到如下结果
==================================
| State Name | Bird Name | Count |
==================================
| Utah | Blue Jay | 5 |
| Utah | Robbin | 0 |
| Arizona | Blue Jay | 0 |
| Arizona | Robbin | 3 |
==================================
请注意,这是我正在尝试做的工作,上面的表格不是我正在使用的实际表格,但它们是我必须使用的结构的一个很好的例子。
我曾试图使用某种左或右连接,但我没有得到我正在寻找的东西。
提前感谢您的帮助。
答案 0 :(得分:0)
我认为这就是你要找的东西。
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;
它首先在所有使用的状态和所有可用的鸟类中进行笛卡尔联合以获得所有可能的组合。然后它连接回原来的三个表来获取鸟类,状态和计数的名称。在这样的连接中,如果计数不可用,则计数将为null,因此我们使用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)