[英]How to get data from two tables like one table all rows and with respect to first table primary key sum of row from other table
[英]Combine two tables together so that all rows from one table are matched with every row from the other
這個問題對我來說很難提出,但我想我想出了一個可以使這個想法傳播的例子。
我有三個表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 |
==================================
請注意,這是我要為工作而做的事情,上面的表並不是我正在使用的實際表,但是它們是我必須使用的結構的一個很好的例子。
我試圖使用某種左或右聯接,但我沒有得到想要的東西。
在此先感謝您的幫助。
我認為這就是您要尋找的。
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
,如果不可用,則計數將為null,因此我們使用IFNULL
函數將所有null都轉換為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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.