0

我试图返回表一 AKA ship_skill_tree中的所有结果,同时匹配表二 AKA character_sheet_skills中找到的行,即使表二中不存在这些行。

SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM  `ship_skill_tree` t
LEFT JOIN  `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID`
LEFT JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID`
WHERE t.`shipTypeID` = 11176 AND c.`character_id` = 1;

表一数据:

|shipTypeID|shipGroupID|skillTypeID|skillLevel
______________________________________________
|11011|26|3332|1
|11129|31|3327|1
|11132|31|3327|1
|11134|31|3327|1
|11172|830|3328|5
|11172|830|12093|1
|11174|893|3328|5
|11174|893|28615|1
|11176|831|3330|5
|11176|831|12092|1

表二数据:

|character_id|typeID|skillpoints|level|published
______________________________________________
|1|3300|1415|2|1
|1|3301|8000|3|1
|1|3327|256000|5|1
|1|3330|2829|2|1
|1|3340|181020|4|1
|1|3341|1024000|5|1
|1|3342|32000|3|1
|1|3343|32202|3|1
|1|3380|256000|5|1
|1|3385|256000|5|1
|1|3386|256000|5|1
|1|3392|256000|5|1
|1|3394|90514|4|1
|1|3402|256000|5|1
|1|3410|768000|5|1
|1|3411|135765|4|1
|1|3412|750|1|1
|1|3413|256000|5|1
|1|3416|45255|4|1
|1|3417|0|0|1
|1|3418|0|0|1
|1|3419|135765|4|1
|1|3420|181020|4|1
|1|3423|0|0|1
|1|3425|90510|4|1
|1|3426|45255|4|1
|1|3428|500|1|1
|1|3429|8000|3|1
|1|3436|45255|4|1
|1|3437|45255|4|1
|1|3438|500|1|1
|1|3449|256000|5|1
|1|3453|0|0|1
|1|3455|256000|5|1
|1|3456|226275|4|1
|1|11579|271530|4|1
|1|12186|0|0|1
|1|12187|0|0|1
|1|12188|0|0|1
|1|12190|22547|3|1
|1|12191|45255|4|1
|1|12192|45255|4|1
|1|12193|45255|4|1
|1|12195|45255|4|1
|1|16281|256000|5|1
|1|17940|1024000|5|1
|1|20342|1280000|5|1
|1|22551|40000|3|1
|1|22578|181020|4|1
|1|25739|0|0|1
|1|26252|16000|3|1
|1|26253|750|1|1
|1|26261|750|1|1
|1|32918|16000|3|1

invTypes 表:

|typeID|typeName
________________
|3327|Spaceship Command
|3328|Gallente Frigate
|3330|Caldari Frigate
|3332|Gallente Cruiser
|12092|Interceptors
|12093|Covert Ops
|28615|Electronic Attack Ships

在上述查询中, shipTypeID将始终或应该始终有效并匹配表一中的记录,但是,在表二中,匹配的行可能不存在。我需要的是输出如下:

|level|levelNeeded|typeName
___________________________
|2|5|Caldari Frigate
|NULL|1|Interceptors

目前这是返回的内容:

|level|levelNeeded|typeName
___________________________
|2|5|Caldari Frigate

编辑:解决方案!

SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM  `ship_skill_tree` t
LEFT JOIN  `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID` AND c.`character_id` = 1
INNER JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID` 
WHERE t.`shipTypeID` = 11176
4

2 回答 2

1

ON您需要对子句中连接的表施加任何限制。如果将它们放在WHERE子句中它不起作用,因为没有任何匹配的行将NULL为这些列生成,并且WHERE子句会将它们过滤掉。

SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM  `ship_skill_tree` t
LEFT JOIN  `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID` AND c.`character_id` = 1
LEFT JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID`
WHERE t.`shipTypeID` = 11176

演示

于 2014-03-31T21:42:05.163 回答
0

您需要使用右连接或外连接而不是左连接。查看SQL 连接的 Visual Representation以获得良好的概述

于 2014-03-31T05:33:18.587 回答