显示需求列表,包括完整的需求列表

时间:2017-06-15 00:04:15

标签: mysql sql select

我在使用这个看似简单的选择查询时遇到了麻烦。

我有三张桌子,一张徽章表,一张要求表,以及一张显示哪些侦察员已完成哪些要求的表格。我希望能够输入徽章#和侦察ID,并获得我输入的徽章的所有要求的清单以及指定侦察员已完成的要求的完成日期。

以下是我的表格:

徽章表

__________________________
|id|badgeName  |rank     |
--------------------------  
|1 |First Aid  |Navigator|
|2 |Woods Tools|Navigator|
|3 |Aquatics   |Navigator|
|4 |Camping    |Navigator|
--------------------------

需求表

____________________________________________________________________
id |badgeid|requirementnum|requirementstep|description              |
---------------------------------------------------------------------
|1 |1      |11            |a              |Requirement 11a First Aid|
|2 |1      |11            |               |Requirement 11 First Aid |
|3 |1      |11            |b              |Requirement 11b First Aid|
|4 |1      |11            |c              |Requirement 11c First Aid|
|5 |1      |11            |d              |Requirement 11d First Aid|
|6 |1      |11            |e              |Requirement 11e First Aid|
|7 |1      |11            |f              |Rquirement 11f First Aid |
|8 |1      |11            |g              |Requirement 11g First Aid|
|9 |1      |1             |               |Requirement 1 First Aid  |
|10|1      |1             |a              |Requirement 1a First Aid |
|11|1      |1             |b              |Requirement 1b First Aid |
|12|1      |2             |               |Requirement 2 First Aid  |
|13|1      |2             |a              |Requirement 2a First Aid |
|14|3      |1             |               |Requirement 1 Aquatics   |
|15|3      |2             |               |Requirement 2 awuatics   |
|16|3      |3             |a              |Requirement 3a Aquatics  |
|17|3      |3             |b              |Requirement 3b Aquatics  |
|18|4      |1             |               |Requirement 1Camping     |
|19|4      |2             |a              |Requirement 2a Camping   |
|20|4      |2             |b              |Requirement 2b camping   |
---------------------------------------------------------------------

Trailman /要求表

________________________________________________
|id|requirementid|trailmanid|complete  |comments|
-------------------------------------------------
|1 |1            |1         |2014-01-01|JO      |
|2 |2            |1         |2014-01-01|JO      |
|3 |3            |1         |2016-01-01|CS      |
|4 |18           |4         |2016-01-01|CS      |
|5 |19           |4         |2016-01-01|CS      |
|6 |20           |4         |2014-02-10|JO      |
|7 |18           |1         |2016-06-12|null    |
|8 |14           |2         |null      |null    |
|9 |15           |2         |null      |null    |
-------------------------------------------------

所以,如果我试图通过trailmanid = 1显示所有野营徽章要求和任何要求的完成日期,我就是这样做的:

select badgename, requirementnum, requirementstep, description,firstname, complete
from badges inner join requirements on badges.id = requirements.badgeid
left join trailmenrequirements on requirements.[id] = trailmenrequirements.requirementid
left join trailmen on trailmenrequirements.[trailmanid] = trailmen.id
where (trailmanid = 1 or trailmanid is null)
and badgeid = 4

这不能得到我想要的东西,它只会拉回一个野营要求,即trailmanid = 1已经完成。如果我做了一些其他组合,就像寻找Trailmanid = 1的急救徽章那么它似乎拉回正确的数据。我认为这与任何侦察员是否已完成某些要求有关。

花费太多时间和100次排列之后,我想我会争取专家。有人可以帮忙吗?

上述查询的预期结果为:

+-----------+----------------+-----------------+------------------------+------------+------------+
| badgename | requirementnum | requirementstep | description            | trailmanid | complete   |
+-----------+----------------+-----------------+------------------------+------------+------------+
| Camping   | 1              | null            | Requirement 1 camping  | 1          | 2016-06-12 |
+-----------+----------------+-----------------+------------------------+------------+------------+
| Camping   | 2              | a               | Requirement 2a Camping |            |            |
+-----------+----------------+-----------------+------------------------+------------+------------+
| Camping   | 2              | b               | Requirement 2b Camping |            |            |
+-----------+----------------+-----------------+------------------------+------------+------------+

我将trailmanid替换为FirstName,因为我没有包含上面的trailman表

0 个答案:

没有答案