2

我根本不是 MySQL 专家,如果有人花一些时间帮助我,我将不胜感激。我有三个表,如下所示:

TEAM(teamID, teamName, userID)
YOUTH_TEAM(youthTeamID, youthTeamName, teamID)
YOUTH_PLAYER(youthPlayerID, youthPlayerFirstName, youthPlayerLastName, youthPlayerAge, youthPlayerDays, youthPlayerRating, youthPlayerPosition, youthTeamID)

这是我现在的查询:

SELECT team.teamName, youth_team.youthTeamName, youth_player.*
FROM youth_player
    INNER JOIN youth_team ON youth_player.youthTeamID = youth_team.youthTeamID
    INNER JOIN team ON youth_team.teamID = team.teamID
WHERE youth_player.youthPlayerAge < 18
AND youth_player.youthPlayerDays < 21
AND youth_player.youthPlayerRating >= 5.5

我想添加到这个查询的是更彻底的检查,如下所示:

  • 如果球员有 16 岁,并且他的位置是得分手,那么球员应该至少有 7 个等级才能被返回
  • 如果球员有 15 年,并且他的位置是组织核心,那么球员应该至少有 5.5 的评分才能被退回
  • 等等等等

如何在我的查询中实现这些要求(如果可能),该查询是否会成为一个糟糕的解决方案?如果我使用 PHP 代码(如果我们假设我使用 PHP)而不是在查询中进行选择,是否会更好?

4

2 回答 2

1

这是带有附加“标准/过滤器”表的可能解决方案:

-- SAMPLE TEAMS: Yankees, Knicks:
INSERT INTO `team` VALUES (1,'Yankees',2),(2,'Knicks',1);

-- SAMPLE YOUTH TEAMS: Yankees Juniors, Knicks Juniors
INSERT INTO `youth_team` VALUES (1,'Knicks Juniors',1),(2,'Yankees Juniors',2);

-- SAMPLE PLAYERS
INSERT INTO `youth_player` VALUES       
  (1,'Carmelo','Anthony',16,20,7.5,'scorer',1),
  (2,'Amar\'e','Stoudemire',17,45,5.5,'playmaker',1),
  (3,'Iman','Shumpert',15,15,6.1,'playmaker',1),
  (4,'Alex','Rodriguez',18,60,3.5,'playmaker',2),
  (5,'Hiroki','Kuroda',16,17,8.7,'scorer',2),
  (6,'Ichiro','Suzuki',19,73,8.3,'playmaker',2);

-- CRITERIA TABLE
CREATE TABLE `criterias` (
  `id` int(11) NOT NULL,
  `age` int(11) DEFAULT NULL,
  `position` varchar(45) DEFAULT NULL,
  `min_rating` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- SAMPLE CRITERIAS
-- AGE=16, POSITION=SCORER, MIN_RATING=7
-- AGE=15, POSITION=PLAYMAKER, MIN_RATING=5.5
INSERT INTO `criterias` VALUES (1,16,'scorer',7), (2,15,'playmaker',5.5);

现在您的查询可能如下所示:

SELECT team.teamName, youth_team.youthTeamName, youth_player.*
FROM youth_player
    CROSS JOIN criterias
    INNER JOIN youth_team ON youth_player.youthTeamID = youth_team.youthTeamID
    INNER JOIN team ON youth_team.teamID = team.teamID
WHERE 
(
    youth_player.youthPlayerAge < 18
    AND youth_player.youthPlayerDays < 21
    AND youth_player.youthPlayerRating >= 5.5
) 
AND
(
    youth_player.youthPlayerAge = criterias.age
    AND youth_player.youthPlayerPosition = criterias.position
    AND youth_player.youthPlayerRating >= criterias.min_rating
)

这产生(缩短的结果):

teamName  youthTeamName     youthPlayerName   Age   Days  Rating  Position   
=============================================================================
Yankees   "Knicks Juniors"  Carmelo Anthony   16    20    7.5     scorer        
Yankees   "Knicks Juniors"  Iman Shumpert     15    15    6.1     playmaker   
Knicks    "Yankees Juniors" Hiroki Kuroda     16    17    8.7     scorer       
于 2013-11-05T14:33:26.677 回答
1

在查询中这样做是很好的......只要它不会太混乱。您可以在查询中执行很多操作,但可能很难维护。因此,如果它变得太长并且您希望其他人查看它,您应该将其拆分或在您的 php-script 中找到解决方案。

至于您的要求,也将其添加到您的 WHERE 部分:

AND 
(
    (YOUTH_PLAYER.youthPlayerAge >= 16 AND YOUTH_PLAYER.youthPlayerPosition = 'scorer' AND YOUTH_PLAYER.youthPlayerRating >= 7)
    OR (YOUTH_PLAYER.youthPlayerAge >= 15 AND YOUTH_PLAYER.youthPlayerPosition = 'playmaker' AND YOUTH_PLAYER.youthPlayerRating >= 5.5)
)
于 2013-11-04T22:49:52.677 回答