【问题标题】:Combine two tables together so that all rows from one table are matched with every row from the other将两个表组合在一起,使一个表中的所有行都与另一个表中的每一行匹配
【发布时间】:2012-04-20 15:37:25
【问题描述】:

这个问题对我来说有点难以表述,但我想我已经想出了一个可以让这个想法得到理解的例子。

我有三个表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   |
==================================

请注意,这是我正在尝试为工作做的事情,上面的表格不是我正在使用的实际表格,但它们是我必须使用的结构的一个很好的例子。

我尝试使用某种左连接或右连接,但没有得到我想要的。

提前感谢您的帮助。

【问题讨论】:

  • @asawyer 我已经查过了,但我不知道如何使用它来获得我正在寻找的结果。如果你能给出一个很好的示例查询。谢谢

标签: mysql join


【解决方案1】:

我想这就是你要找的。​​p>

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)

【讨论】:

  • 哇,它真的很复杂,但对我有用。现在我只需要把它翻译成我的实际表格。谢谢。
猜你喜欢
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 2017-05-02
  • 2012-12-20
  • 2022-11-20
相关资源
最近更新 更多