所以我试图在Mysql中创建一个使用inner join
从多个表中获取多个信息的查询,但是,我无法使其工作。基本上,我想知道每个师中有多少战斗员,并显示CO名称,分部名称和FOB。对于这个任务,我假设每个CO负责一个师和师内的一切,每个团有与其旅相同的FOB,所以,我必须结合3个表来得到我的结果查询。我的表格如下:
ArmyRegiment表:
+---------------------------------------------+---------------------+----------------------+----------------+------------+
| Name | Codename | Type | FOB | Combatants |
+---------------------------------------------+---------------------+----------------------+----------------+------------+
| 12 Régiment blindé du Canada | Adsum | Armour | CFB Valcartier | 9 |
| 5 Régiment d'artillerie légère du Canada | Ubique | Artillery | CFB Valcartier | 12 |
| 5 Combat Engineer Regiment | | Support | CFB Valcartier | 5 |
| 3rd Battalion, Royal 22e Régiment | Vipère | Light Infantry | CFB Valcartier | 5 |
| The Canadian Grenadier Guards | | Light Infantry | Montreal | 5 |
| Les Fusiliers Mont-Royal | | Light Infantry | Montreal | 7 |
| The Royal Canadian Hussars | Red Bird | Reconnaissance | Montreal | 10 |
| Royal Canadian Horse Artillery | Thunder | Artillery | CFB Edmonton | 10 |
| Lord Strathcona's Horse | Strat | Armour | CFB Edmonton | 15 |
| 1 Service Battalion | Angel 1 | Combat Service Suppo | CFB Edmonton | 5 |
| The Saskatchewan Dragoons | Dragoon | Armour Reconnaissanc | Winnipeg | 8 |
| The Royal Winnipeg Rifles | Little Black Devils | Light Infantry | Winnipeg | 5 |
| 38 Service Battalion | Little Black Devils | Combat Servicee Supp | Winnipeg | 10 |
| 20th Field Artillery Regiment | | Artillery | Calgary | 11 |
| 41 Combat Engineer Regiment | | Support | Calgary | 5 |
| 1st Battalion, The Royal Canadian Reg | Royal Canadians | Mechanized infantry | CFB Petawawa | 6 |
| 2 Combat Engineer Regiment | Burner | Support | CFB Petawawa | 5 |
| The Royal Highland Fusiliers of Canada | Highland Laddies | Light Infantry | London | 7 |
| The Royal London Light Infantry | The Rileys | Light Infantry | London | 6 |
| The Governor General's Horse Guards | Gugga Huggas | Reconnaissance | Toronto | 6 |
| The Queen's Own Rifles of Canada | | Airborne Light Infan | Toronto | 10 |
| 48th Highlanders of Canada | | Light Infantry | Toronto | 5 |
| 84th Independent Field Battery | | Artillery | Halifax | 6 |
| 36 Combat Engineer Regiment | Red Clovers | Support | Halifax | 5 |
| The West Nova Scotia Regiment | | Light Infantry | Halifax | 6 |
| The Royal New Brunswick Regiment | Rider | Light Infantry | Moncton | 6 |
| 37 Combat Engineer Regiment | | Support | Moncton | 8 |
| 8th Canadian Hussars | The Crazy Eights | Reconnaissance | Moncton | 10 |
+---------------------------------------------+---------------------+----------------------+----------------+------------+
ArmyBrigade表:
+---------------------------------------+------------------------+----------------+
| Name | CO | FOB |
+---------------------------------------+------------------------+----------------+
| 36th Canadian Brigade Group | MGen B.M. Hoffmeister | Halifax |
| 37th Canadian Brigade Group | MGen B.M. Hoffmeister | Moncton |
| 2nd Canadian Mechanized Brigade Group | Gen George Kitching | CFB Petawawa |
| 31 Canadian Brigade Group | Gen George Kitching | London |
| 32 Canadian Brigade Group | Gen George Kitching | Toronto |
| 1st Canadian Mechanized Brigade Group | Brigadier-Gen W.D Eyre | CFB Edmonton |
| 38 Canadian Brigade Group | Brigadier-Gen W.D Eyre | Winnipeg |
| 41 Canadian Brigade Group | Brigadier-Gen W.D Eyre | Calgary |
| 5th Canadian Mechanized Brigade Group | MGen Sam Steeles | CFB Valcartier |
| 34 Canadian Brigade Group | MGen Sam Steeles | Montreal |
+---------------------------------------+------------------------+----------------+
ArmyDivision表:
+-----------------------+------------------------+--------------+
| Name | CO | FOB |
+-----------------------+------------------------+--------------+
| 3rd Canadian Division | Brigadier-Gen W.D Eyre | |
| 2nd Canadian Division | MGen Sam Steeles | Quebec |
| 1st Canadian Division | MGen Dean Milner | CFB Kingston |
| 4th Canadian Division | Gen George Kitching | Ontario |
| 5th Canadian Division | MGen B.M. Hoffmeister | |
+-----------------------+------------------------+--------------+
这就是我一直在尝试使用的代码,这是我学会了做的(虽然更简单的版本):
select sum(r.combatants) Personal, b.CO, r.FOB from ArmyRegiment r, ArmyBrigade b where b.FOB=r.FOB group by b.CO join inner ArmyDivison d on d.CO = ArmyBrigade.CO;
但是我收到了这个错误:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'join inner ArmyDivison d on d.CO = ArmyBrigade.CO' at line 1
答案 0 :(得分:0)
以下是基于问题中查询语法中引用的表和列来连接三个表的有效语法示例...
假设FOB
列在ArmyBrigade
中是唯一的,而CO
列在ArmyDivision
中是唯一的,
SELECT d.CO AS ArmyDivision_CO
, d.FOB AS ArmyDivision_FOB
, IFNULL(SUM(r.combatants),0) AS total_combatants
FROM ArmyDivision d
LEFT
JOIN ArmyBrigade b
ON b.CO = d.CO
LEFT
JOIN ArmyRegiment r
ON r.FOB = b.FOB
GROUP BY d.CO, d.FOB
鉴于样本数据,我们希望这会返回如下结果集:
+------------------------+------------------+------------------+
| ArmyDivision_CO | ArmyDivision_FOB | total_combatants |
+------------------------+------------------+------------------+
| Brigadier-Gen W.D Eyre | | |
| Gen George Kitching | Ontario | |
| MGen B.M. Hoffmeister | | |
| MGen Dean Milner | CFB Kingston | |
| MGen Sam Steeles | Quebec | |
+------------------------+------------------+------------------+
请注意,这将返回ArmyDivision中每一行的战斗员总数,但如果ArmyRegiment中的任何行与ArmyBrigade中的行不匹配,或者ArmyBrigade中与ArmyDivision中的行不匹配的任何行,则那些不匹配的行不会包含在total_combatants中。